Text Class Error - '.' token, expected unqualified-id

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;
} ///:~ 
Look at line 5 in that snippet you posted (which, I assume, is actually line 74 in your complete code). I assume TMA2Question3_TEXT.cpp is supposed to actually be the text in a string?
Last edited on
Hey MikeyBoy, Line 5 is my attempt to make a constructor that takes a string argument that is the name of the file to open.
Yes - so TMA2Question3_TEXT.cpp is actually supposed to be the contents of a string, right? Rather than the name of a variable, or some other entity?

Right now, the way you've written line 5 would mean that TMA2Question3_TEXT is the name of some entity (an object, or a struct) that has a member called cpp.

You obviously know the syntax for using strings, because you do it correctly on the very next line.
So, you're suggesting I just put quotes around the file name?
If I do that, I get:

$ c++ -o TMA2Question3 TMA2Question3.cpp
TMA2Question3.cpp:74:8: error: expected unqualified-id before string constant
Text("TMA2Question3_TEXT.cpp") {
^
TMA2Question3.cpp:74:8: error: expected ‘)’ before string constant
TMA2Question3.cpp:93:1: error: expected ‘}’ at end of input
} ///:~
^
TMA2Question3.cpp:93:1: error: expected unqualified-id at end of input
So having looked at your code a bit more closely, I can see the confusion. On line 5 you are defining a method (in this case, a constructor). The first part of that definition must define the name of the method, and the arguments you're passing in. To define an argument, you must define what type it is, and the name of it.

In your line 5, you (a) don't supply a type, and (b) supply a name that isn't legal, because of the . in the middle of it.

However, even if you fix that, it all seems pointless, because nowhere in that constructor do you ever use that argument. On line 6, you use a hard-coded string for the name of the file you're opening, completely ignoring the argument that's been passed in. So why bother passing it in at all?

I'd also add that the name of the argument isn't very helpful. An argument - or any other variable - should be given a name that's descriptive of its purpose. So something like "fileName" would be much better.
Alright, I took your suggestions into consideration when editing my code, and it seemed to be fixed, except my ifstream isn't reading in the text file. Any Suggestions?

New Code:
class Text {
string text;
public:
Text() {}; //default constructor
Text(const string& TMA2Question3_TEXT) { //constructor with filename argument
string s1; // empty string
ifstream input_file("TMA2Question3_TEXT.cpp"); //ifstream of text file
for(int i = 0; i < 10; i++) { // for loop
(getline(input_file, s1));
text = s1;}
};

string contents() { // contents function
return text;
}
};

int main() {
Text t1; //text object 1
Text t2; //text object 2
cout << "t1: " << t1.contents() << endl; //print statements
cout << "t2: " << t2.contents() << endl;
} ///:~



Output:
$ ./TMA2Question3
t1:
t2:



Sorry about the naming, but for the purpose of this program, the naming of the argument is a requirement.
Well, firstly, I have to repeat my previous comment:

MikeyBoy wrote:
However, even if you fix that, it all seems pointless, because nowhere in that constructor do you ever use that argument. On line 6, you use a hard-coded string for the name of the file you're opening, completely ignoring the argument that's been passed in. So why bother passing it in at all?


You're still totally ignoring the argument that you're passing into the constructor. So what's the point of it?

In fact, nowhere in your code do you use the constructor that takes the filename as argument. Both t1 and t2 are created using the default constructor. And your default constructor does nothing - you've defined it with an empty block. No wonder it's not reading anything!

Alright, I'm sorry if its frustrating trying to convey a simple problem to me, but I am completely new to programming, so I am trying my best.

At first, I tried to give the ifstream the filename TMA2Question3_TEXT for an argument, but the compiler complained because ifstream only takes a const char*. So I tried to implement appending .c_str() to the end of the file name (TMA2Question3_TEXT.c_str()), in order to convert the string to a const char*. Still no luck.

Am I properly using the argument for the constructor with the argument name now? If not, can you please send an example code snippet that might help?

Thanks
You are properly specifying the name of an argument for your constructor. That argument is a reference to a variable whose type is const string and whose name is TMA2Question3_TEXT.

But you never, ever, use that argument. Anywhere. There's no point passing a string into the constructor, because you never, ever use that string. Instead, inside the constructor, you're hard-coding the name of the file as "TMA2Question3_TEXT.cpp".

Why bother passing a filename into the constructor, and never using it?

Then, as another isssue, look at how you're creating your objects:

1
2
Text t1; //text object 1
Text t2; //text object 2 


You're not even using the constructor that takes an argument! You're using the default constructor.

And look how you've defined that default constructor:

Text() {}; //default constructor

It doesn't do anything. It's empty. It doesn't open a file, or read it, or store anything in text.
Last edited on
Topic archived. No new replies allowed.