Compilation Error:Class name does not name a type.

S

sharat

hi groups,

i have created 2 .cpp and .h(header) file and one main.cpp file, such
as file1.cpp and .h similarly file2.cpp & .h .
i have include the apropriate header files also .now i am creating the
object of class A (which is declare and define in file1.h) into class
B(which is declare and define in file2.h). i am getting an error that
"class a " doesnot name a type and the object i have creted is
undeclared . i am unable to get why this error is coming ,as i have
includede the related header files also.

can any body help to solve this problem.

Thanx in advance...
 
I

Ian Collins

sharat said:
hi groups,

i have created 2 .cpp and .h(header) file and one main.cpp file, such
as file1.cpp and .h similarly file2.cpp & .h .
i have include the apropriate header files also .now i am creating the
object of class A (which is declare and define in file1.h) into class
B(which is declare and define in file2.h). i am getting an error that
"class a " doesnot name a type and the object i have creted is
undeclared . i am unable to get why this error is coming ,as i have
includede the related header files also.

can any body help to solve this problem.
Please post a minimal example that gives your problem. Otherwise all
you can hope for is a guess.
 
S

Salt_Peter

hi groups,

i have created 2 .cpp and .h(header) file and one main.cpp file, such
as file1.cpp and .h similarly file2.cpp & .h .
i have include the apropriate header files also .now i am creating the
object of class A (which is declare and define in file1.h) into class
B(which is declare and define in file2.h). i am getting an error that
"class a " doesnot name a type and the object i have creted is
undeclared . i am unable to get why this error is coming ,as i have
includede the related header files also.

can any body help to solve this problem.

Thanx in advance...


Not unless you show code.
Note that < class A > and < class a > do not correspond.
So the error is probably a fundamental one.
 
S

sharat

here is my code ..

//file1.cpp
using namespace std;
#include "file1.h"
void plot_class::fun1()
{
cout<<"\n PLOT CLASS \n";
}

//file1.h
#ifndef FILE1_H
#define FILE_H

#include"file2.h"
class plot_class
{
public:
int p;
zone_class zone_obj;
void fun1();

};
#endif

similarly ,
//file2.cpp
#include"file2.h"
void zone_class ::fun_zone()
{
cout<<"\n zone class \n";
}

//file2.h
#ifndef FILE2_H
#define FILE2_H
#include"file1.cpp"

class zone_class
{
public:
int z;
plot_class plot_obj;
void fun_zone();
};
#endif

and main.cpp is as follows.

#include<iostream>
#include"file1.cpp"
#include"file2.cpp"
using namespace std;
int main()
{
cout<<"\n Hello World\n";
zone_class zone_obj1;

zone_obj1.fun_zone();

}

i am using gcc compiler ..after compiling i am getting the following
errors
$ g++ main.cpp


file2.h:9: error: 'plot_class' does not name a type
 
A

Alf P. Steinbach

* sharat:
here is my code ..

//file1.cpp
using namespace std;
#include "file1.h"

Preferentially include the header file before /anything/ else, so that
you get a check (however fallible) that the header file is OK on its own.

void plot_class::fun1()
{
cout<<"\n PLOT CLASS \n";
}

You haven't included <iostream> and <ostream>. OK, you have, in the
main program, but that's a bug masking a bug.

//file1.h
#ifndef FILE1_H
#define FILE_H
Typo.


#include"file2.h"
class plot_class
{
public:
int p;
zone_class zone_obj;
void fun1();

};
#endif

similarly ,
//file2.cpp
#include"file2.h"
void zone_class ::fun_zone()
{
cout<<"\n zone class \n";
}

//file2.h
#ifndef FILE2_H
#define FILE2_H
#include"file1.cpp"

Typo or thinko.

Don't include implementation files, ever.

class zone_class
{
public:
int z;
plot_class plot_obj;
void fun_zone();
};
#endif

and main.cpp is as follows.

#include<iostream>
#include"file1.cpp"

Don't include implementation files, ever.

#include"file2.cpp"

Don't include implementation files, ever.

using namespace std;
int main()
{
cout<<"\n Hello World\n";
zone_class zone_obj1;

zone_obj1.fun_zone();

}

i am using gcc compiler ..after compiling i am getting the following
errors
$ g++ main.cpp


file2.h:9: error: 'plot_class' does not name a type

In [main.cpp] you include first of all [file1.cpp], which includes
[file1.h], which includes [file2.h], which at line references
plot_class, which so far hasn't been declared or defined.
 
S

Serge Paccalin

Le 19.04.2007 11:02, sharat a ecrit:
here is my code ..

class plot_class
{
public:
int p;
zone_class zone_obj;
void fun1();

};

So, a plot_class contains an int and a zone_class...
class zone_class
{
public:
int z;
plot_class plot_obj;
void fun_zone();
};

So, a zone_class contains an int and a plot_class...

Question: assuming an int is 4 bytes and there is no extra byte wasted,
can you compute the size of a plot_class? the size of a zone_class?

Do you see your problem now?

--
___________
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Pour bien répondre avec Google, ne pas cliquer
-'(__) « Répondre », mais « Afficher les options »,
_/___(_) puis cliquer « Répondre » (parmi les options).
 
A

anon

Serge said:
Le 19.04.2007 11:02, sharat a ecrit:


So, a plot_class contains an int and a zone_class...


So, a zone_class contains an int and a plot_class...

Question: assuming an int is 4 bytes and there is no extra byte wasted,

Extra byte wasted?
can you compute the size of a plot_class? the size of a zone_class?

How can it matter?
Do you see your problem now?

I don't
 
S

Serge Paccalin

Le 19.04.2007 11:16, anon a ecrit:
Extra byte wasted?

For alignment purposes, for instance. "Wasted" is probably not the
correct word, but English is not my native language either.
How can it matter?

Just try it.

Circularity.

--
___________
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Pour bien répondre avec Google, ne pas cliquer
-'(__) « Répondre », mais « Afficher les options »,
_/___(_) puis cliquer « Répondre » (parmi les options).
 
A

anon

Serge said:
Le 19.04.2007 11:16, anon a ecrit:


For alignment purposes, for instance. "Wasted" is probably not the
correct word, but English is not my native language either.
I do not see where a byte can be wasted there, and how can it influence
the error he is getting.

Just try it.


Circularity.

Do you mean circularity with includes or what?
He should have declared classes, instead of including header files
(including source files is out of question here)
Like this:

//file1.h
#ifndef FILE1_H
#define FILE1_H

class zone_class;

class plot_class
{
public:
int p;
zone_class zone_obj;
void fun1();

};
#endif

Similar for file2.h
 
A

anon

Ian said:
class A has a member of type class B. Class B has a member of type
class A. How big is class A?

Thanks for clarifications :)
I knew I have used something similar, but references instead of objects
 
A

Alf P. Steinbach

* sharat:
still not clear

In C++, if an object A contains another object B, it really directly
contains object B.

The size of object A is the size of its member object B plus the sizes
of whatever other members there are in object A, pluss the size of
compiler-inserted padding, if any.

Now, let type Y have some data member such that its size is at least 1.
If any object of type X contains an object of type Y, which in turn
contains an object of type X, which..., what's the size of an object of
type X? What's the size of an object of type Y?
 
A

anon

sharat said:
still not clear

You need to use pointers or references, instead of actual objects inside
your classes. Like in these headers:

//file1.h
#ifndef FILE1_H
#define FILE1_H

class zone_class;
class plot_class
{
public:
int p;
zone_class& zone_obj;
void fun1();

};
#endif

//file2.h
#ifndef FILE2_H
#define FILE2_H

class plot_class;
class zone_class
{
public:
int z;
plot_class& plot_obj;
void fun_zone();
};
#endif

Otherwise, object of class zone_class would contain an object of
plot_class class, which contains object of zone_class class, ... , to
infinity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top