Hey Guys,
So I am new here, and am totally new to programming. I am trying to take a c++ programming course, and to be completely honest, am lost half the time. Could anyone help me with my code? I am trying to do the following:
"Write a program in which you create a Text class that contains a string object to hold the text of a file. Give it two constructors: a default constructor and a constructor that takes a string argument that is the name of the file to open. When the second constructor is used, open the file and read the contents of the file into the string member object. Add a member function contents() to return the string so that you can display it. In main(), open a file using Text and display the contents."
My problem: I keep getting the error message:
"TMA2Question3.cpp:74:26: error: expected ‘)’ before ‘.’ token
Text(TMA2Question3_TEXT.cpp) {
^
TMA2Question3.cpp:93:1: error: expected ‘}’ at end of input
} ///:~
^
TMA2Question3.cpp:93:1: error: expected unqualified-id at end of input"
I have tried to get rid of the . in line 74, but it just gives me another error of "TMA2Question3.cpp:81:3: error: field ‘TMA2Question3_TEXT’ has incomplete type };"
I apologize if this is a trivial question, but any suggestions to fix the problem and an explanation of why the problem happened (to avoid these problems in the future), correct my sloppy coding, etc. would be greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class Text {
string text;
public:
Text();
Text(TMA2Question3_TEXT.cpp) {
ifstream in("TMA2Question3_TEXT.cpp");
string s;
while(getline(in, s)) { // Discards newline char
cout << s; // Adds it back
cin.get();
}
};
string contents() {
return text;
}
};
int main() {
Text t1;
Text t2;
cout << "t1: " << t1.contents() << endl;
cout << "t2: " << t2.contents() << endl;
} ///:~
|