1

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");
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
beginh
  • 1,133
  • 3
  • 26
  • 36
  • 3
    Your empty constructor is not defined. Either define it yourself, or use `Text() = default` if you are using c++11. You have declared `Text::Text();` without providing definition, at least I can't see it in the posted code... – tmaric Mar 07 '14 at 12:31
  • 2
    I don't think the error message saying that there is an undefined reference **to** `main`, but rather that there is an undefined reference **in** `main`. In the future when posting questions about compiler or linker errors, please include the *complete* and *unedited* error log in the question. – Some programmer dude Mar 07 '14 at 12:35
  • error LNK2019: unresolved external symbol "public: __thiscall Text::Text(void)" – Leo Chapiro Mar 07 '14 at 12:37
  • oh, thanks, i'm so stupid! I need to find way to interpret error messages correctly so I can trace down the causes.. – beginh Mar 07 '14 at 12:41
  • @beginh You can use the int main(int argc...etc) with Visual Studio. See here http://stackoverflow.com/questions/3697299/passing-command-line-arguments-in-visual-studio-2010 – const_ref Mar 07 '14 at 12:59

2 Answers2

2

As tmaric already says, you need an empty constructor:

//Text();
Text(){};
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
1

You have to define your empty destructor.

user2076694
  • 806
  • 1
  • 6
  • 10