문제

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