質問

I want to test constructor's functionality and an interesting problem was encountered. After compiling that code i get the linking error LNK2019 referencing to main(). How to be able to read and copy the content of one.txt line by line in my case? I'm referring to the book "Thinking in C++" ex 7.01 but since working with Visual Studio I cannot use main(int argc, char* argv[]) version..

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Text
{
    string text;
public:
    Text();
    Text(const string& name) 
    {
        ifstream infile;
        infile.open(name);
        if(!infile.good())
        {
            cout << "File is not open";
        }
        else
        {
            string line;
            while(getline(infile,line))
                text = text + line + '\n';
        }
    };
    string contetns()
    {
        return text;
    };
};

int main()
{
    Text o1;
    Text o2("one.txt");
    cout << "content: " << o1.contetns() << endl;
    cout << "content: " << o2.contetns() << endl;
    system("pause");
}
役に立ちましたか?

解決

As tmaric already says, you need an empty constructor:

//Text();
Text(){};

他のヒント

You have to define your empty destructor.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top