- 💌 Affiliated Columns:[Program Error Solution (Recommended)]
- 😀 Writer: I'm a dog in the dead of night.
- 🚀 Personal Profile: a CV engineer who is trying to learn technology, focusing on basic and practical sharing , welcome to consult!
- 💖 Welcome: this is CSDN, the place where I summarize my knowledge, please triple link if you like, and private message if you have any questions 😘 😘 😘 😘 😘
Your likes, followers, favorites, and comments are the biggest incentive and support for me!!!! 🤩 🤩 🤩 🤩
There are some C++ programs that run with some errors on vs2017 and run out in vc6.0. The next program is the one that I am getting errors running on vs2017 and running out on vc6.0. But I have solved the problem by analyzing the source of the error on vs2017 and sharing it as such.
Instance Code:
#include<iostream>.
using namespace std;
class String
{
public.
String() { p = NULL; } //define default constructor
String( char *str); // Declare the constructor.
void display();
private.
char *p; //Character pointer to String
};
String::String( char *str) //define constructor
{
p = str; // make p point to the real reference string
}
void String::display()
{
cout << p;
}
int main()
{
String string1("Helle"); String string2("Book");
String string2("Book").
();
cout << endl.
(); cout << endl;
String string2("Book"); (); cout << endl; (); cout << endl
cout << endl; return 0;
}
Running it in vs2017 will show
From the error shown in vs2017, it is clear that if you change the formal parameter to a constant pointer, it will match the real parameter, so you can change the source program as follows:
#include<iostream>;
#include<string>
using namespace std.
class String
{
public.
String() { p = NULL; } //define the default constructor
String(const char *str); // Declare the constructor, defining the formal parameter as a constant pointer.
void display(); //Declare the constructor, defining the formal parameter as a constant pointer.
private.
const char *p; //define as a constant object member to point to the string
};
String::String(const char *str) //define constructor
{
p = str; // make p point to the real reference string
}
void String::display()
{
cout << p;
}
int main()
{
String string1("Helle"); String string2("Book");
String string2("Book").
();
cout << endl.
(); cout << endl;
String string2("Book"); (); cout << endl; (); cout << endl
cout << endl; return 0;
}
This will run out successfully in vs2017