expected unqualified-id before '{' token

hi there.
i just started studying c++ and i was doing an excercise when i started having these problems with classes.
I'm using Dev-C++ to write my code since i found it very simple, and i'm on windows 10.

Errors:
[Error] expected unqualified-id before '{' token (in GradeBook.h line 6, col 1)
[Error] variable 'GradeBook mio' has initializer but incomplete type (in main line 8, col 15)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(File main.cpp)

#include <iostream>
#include "GradeBook.h"
using std::cout;
using std::endl;

int main()
{
	GradeBook mio("Informatica");
	
	
	system("pause");
	return 0;
}

file GradeBook.h

//GradeBook.h
#include <string>
using std::string;

class GradeBook
{
	private:
		string nomeCorso;
		//string nomeDocente;
	public:
		GradeBook(string);
		void setNomeCorso(string);
		string getNomeCorso();
		void displayMessage();
	
};

(file GradeBook.cpp)

//GradeBook.cpp
#include <iostream>
#include "GradeBook.h"
#include <string>
using std::string;
using std::cout;
using std::endl;

GradeBook::GradeBook(string name)
{
	setNomeCorso(name);
}

void GradeBook::setNomeCorso(string name)
{
	nomeCorso=name;
}

string GradeBook::getNomeCorso()
{
	return nomeCorso;
}

void GradeBook::displayMessage()
{
	cout<<"Nome corso: "<<getNomeCorso()<<endl;
}


is there anyone who knows what i am doing wrong?
Last edited on
line 61: displayMessage() typo, g not gg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

class GradeBook
{
private:
    string nomeCorso;

public:
    GradeBook(string);
    ~GradeBook(){};
    
    void setNomeCorso(string);
    string getNomeCorso();
    void displayMessage();
};

GradeBook::GradeBook(string name)
{
    nomeCorso = name;
}

void GradeBook::setNomeCorso(string name)
{
    nomeCorso = name;
}

string GradeBook::getNomeCorso()
{
    return nomeCorso;
}

void GradeBook::displayMessage()
{
    cout << "Nome corso: "<< getNomeCorso() << endl;
}

int main()
{
    GradeBook mio("Informatica");
    cout << mio.getNomeCorso() << '\n';
    mio.displayMessage();
    
    return 0;
}


Informatica
Nome corso: Informatica
Program ended with exit code: 0
done, i didn't see it but still no changes in the errors
We crossed over posts so see my version. If you keep the program in the (usual) 3 separate files just split it up accordingly and #include in each file as you need them for that file alone.
Last edited on
~GradeBook(){}; what is this?
i tried to make the changes like you, but still it doesn't work
it gives the same error
it must be said that i am working on 3 separated files in the same folder
Last edited on
It's the destructor - you can read up on it.

3 files as follows:

driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

#include "GradeBook.h"


int main()
{
    GradeBook mio("Informatica");
    cout << mio.getNomeCorso() << '\n';
    mio.displayMessage();
    
    return 0;
}


GradeBook.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>

using std::string;

class GradeBook
{
private:
    string nomeCorso;

public:
    GradeBook(string);
    ~GradeBook(){};
    
    void setNomeCorso(string);
    string getNomeCorso();
    void displayMessage();
};


GradeBook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include "GradeBook.h"

using std::cout;
using std::endl;

GradeBook::GradeBook(string name)
{
    nomeCorso = name;
}

void GradeBook::setNomeCorso(string name)
{
    nomeCorso = name;
}

string GradeBook::getNomeCorso()
{
    return nomeCorso;
}

void GradeBook::displayMessage()
{
    cout << "Nome corso: "<< getNomeCorso() << endl;
}

... and then some bella musica ...
Output:

Informatica
Nome corso: Informatica
Program ended with exit code: 0
okay i swear i'm even doing ctrl+c / v
i tried your code in a new folder with new files now i have new errors
main.cpp:(.text+0x45): undefined reference to `GradeBook::GradeBook(std::string)'
main.cpp:(.text+0x6d): undefined reference to `GradeBook::getNomeCorso()'
main.cpp:(.text+0xa5): undefined reference to `GradeBook::displayMessage()'
[Error] ld returned 1 exit status
Your path needs to be set up so the GradeBook files can be found.

I use XCode on a Mac so VStudio(? I assume) is a long lost memory for me. There's probably some sort of Add file function in the project menu?

You might also have success by putting all 3 files together in the same directory with.

Beyond that wait for help or slap the 3 together again he way I did first time around.

BTW: My 'slapped together' version runs on cppshell here, just press the Edit & Run gear wheel.

Failing all of that buy a Mac :(
well thank you for the effort :), i was using a dev c++, but yes i think i will change to visual studio.
Last edited on
I think most people here will tell you dev c++ is rubbish, CodeBlocks not much better - so VS would definitely be a good move. (Cheaper than a Mac too!)
yo i installed visual studio and it works.
Thank you very much for your help.
(after all the time i spent on trying to resolve this problem i nearly cried when i saw the console)
:)
Topic archived. No new replies allowed.