Could not figure out problem with this code

class GradedActivity
{
protected:
char letter;
virtual void func();

public:

char getLetter()
{
return letter;
}

};

#include "gradedactivity.h"

void gradedactivity::func()
{
cout << "This is func function of gradedactivity" <<endl;

}

#include "gradedactivity.h"
class passfailactivity:public gradedactivity {
protected:
float passingscore;
virtual void func();
public:
void msg();

};

#include "passfailactivity.h"

void passfailactivity::msg()

{


cout << "In msg of passfailactivity" <<endl;


}


void passfailactivity::func()
{
cout << "This is func function of passfailactivity" <<endl;

}

#include "passfailactivity.h"

using namespace std;

int main()
{
passfailactivity e;

e.msg();

e.func();

return 0;
}


I get these error on trying to run this on g++ under linux:

g++ prog.cpp -o prog
In file included from prog.cpp:1:
passfailactivity.h:2: syntax error before `{' token
passfailactivity.h:5: virtual outside class declaration
passfailactivity.h:6: syntax error before `public'
prog.cpp: In function `int main()':
prog.cpp:7: aggregate `passfailactivity e' has incomplete type and cannot be
defined

Can you help me ?

it's saying there is syntax errors in "passfailactivity.h" is this the file you just posted?
if not can you post it and please include code tags this time. the button that looks like <> over on the right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This is gradedActivity.h
class gradedactivity 
{
protected:
char letter;
virtual void func();

public:

char getLetter()
{
return letter;
}

};


1
2
3
4
5
6
7
8
// This is GradedActivity.cpp
#include "gradedactivity.h"

void gradedactivity::func()
{
cout << "This is func function of gradedactivity" <<endl;

}


1
2
3
4
5
6
7
8
9
10
//This is passfailactivity.h
#include "gradedactivity.h"
class passfailactivity:public gradedactivity {
protected:
float passingscore;
virtual void func();
public:
void msg();

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//This is passfailactivity.cpp
#include "passfailactivity.h"

void passfailactivity::msg()

{


cout << "In msg of passfailactivity" <<endl;


}


void passfailactivity::func()
{
cout << "This is func function of passfailactivity" <<endl;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// This is main program prog.cpp
#include "passfailactivity.h"

using namespace std;

int main()
{
passfailactivity e;

e.msg();

//e.func();

return 0;
}


Now I am getting

g++ prog.cpp -o prog
/tmp/cc2HOyTl.o(.text+0x27): In function `main':
: undefined reference to `passfailactivity::msg()'
/tmp/cc2HOyTl.o(.gnu.linkonce.t._ZN16passfailactivityC1Ev+0x19): In function `passfailactivity::passfailactivity()':
: undefined reference to `vtable for passfailactivity'
/tmp/cc2HOyTl.o(.gnu.linkonce.t._ZN14gradedactivityC2Ev+0x8): In function `gradedactivity::gradedactivity()':
: undefined reference to `vtable for gradedactivity'
collect2: ld returned 1 exit status
I added to .h files:

#include <iostream>
using namespace std;

and it compiled fine for me.

What compiler/IDE do you use?
Last edited on
I am using g++ on Linux.

Still does not work for me !! Can you try my code on Linux please , if you have a machine handy ?
sure brb.
yep that works fine, add the include <iostream> to your header files: and compile like this:
1
2
3
4
5
6
7
8
9
10
$
$ g++ -c GradedActivity.cpp
$
$ g++ -c passfailactivity.cpp
$
$ g++ passfailactivity.o GradedActivity.o prog.cpp
$
$ ./a.out
$
$ In msg of passfailactivity


if your using an IDE like Eclipse or CodeBlocks then it should do that for you, assuming everything is in the same project.
Last edited on
Yes it works !

The mistake I was doing was that , I was not creating object files first. I was directly running the command :

g++ prog.cpp -o prog

Thanks all for your help !
Topic archived. No new replies allowed.