Templated container members

A

axel22

Hello,
I have the following problem. I create a class called MyClass which
includes another class calles MyContainer. Class MyContainer has a
member which is a vector, instantiated as vector<MyClass>. When trying
to compile this I receive an error. Here is how it goes:


---------------
MyClass.h
---------------

#pragma once

#include "MyContainer.h"

class MyClass {
private:
int myInteger;
public:
MyClass(void);
~MyClass(void);
};



The reason why MyContainer.h is included here is because I would later
like to make
it a private member of MyClass.


---------------------
MyContainer.h
---------------------

#pragma once

#include "MyClass.h"
#include <vector>

using namespace std;

class MyContainer {
private:
vector<MyClass *> myVector;
MyClass *pointer;
public:
MyContainer(void);
~MyContainer(void);
};




-------------
main.cpp
-------------

#include <iostream>

#include "MyClass.h"
#include "MyContainer.h"

void main() {
MyClass myInstance;
MyContainer myContainerInstance;
}




Output goes like this:


------ Build started: Project: Probarka, Configuration: Debug Win32
------

Compiling...
main.cpp
f:\C++\Projects\Probarka\MyContainer.h(10) : error C2065: 'MyClass' :
undeclared identifier
f:\C++\Projects\Probarka\MyContainer.h(10) : error C2059: syntax error
: '>'
f:\C++\Projects\Probarka\MyContainer.h(15) : error C2143: syntax error
: missing ';' before '}'
f:\C++\Projects\Probarka\MyClass.h(5) : error C2143: syntax error :
etc. etc.




To sum it up: why can't I create a class which has a template member
instantiated with another class which includes the first class? How can
I solve this problem?
 
V

Victor Bazarov

I have the following problem. I create a class called MyClass which
includes another class calles MyContainer. Class MyContainer has a
member which is a vector, instantiated as vector<MyClass>. When trying
to compile this I receive an error. Here is how it goes:


---------------
MyClass.h
---------------

#pragma once

#include "MyContainer.h"

class MyClass {
private:
int myInteger;
public:
MyClass(void);
~MyClass(void);
};



The reason why MyContainer.h is included here is because I would later
like to make
it a private member of MyClass.

So, why don't you do it later, when you actually make your container
the private member of MyClass? If you code using "what if" rule, your
code does not reflect your current design nor the implementation.
---------------------
MyContainer.h
---------------------

#pragma once

#include "MyClass.h"

Don't include "MyClass.h" here. Use the forward declaration of MyClass
class.
#include <vector>

using namespace std;

class MyContainer {
private:
vector<MyClass *> myVector;
MyClass *pointer;
public:
MyContainer(void);
~MyContainer(void);
};
[..]

V
 
N

neo

make some correction in your code as follows:

MyClass.h
--------------------
#pragma once

class MyClass
{
public:
MyClass(void);
public:
~MyClass(void);
private:
int myInteger;
};

MyContainer.h
-------------------------
#pragma once

#include "MyClass.h"
#include <vector>

using namespace std;

class MyContainer
{
public:
MyContainer(void);
public:
~MyContainer(void);
private:
vector<MyClass *> myVector;
MyClass *pointer;
};

you dont need to include "MyContainer.h" in MyClass.h.

regards,
-aims
 
J

Jon Clements

Hello,
I have the following problem. I create a class called MyClass which
includes another class calles MyContainer. Class MyContainer has a
member which is a vector, instantiated as vector<MyClass>. When trying
to compile this I receive an error. Here is how it goes:


---------------
MyClass.h
---------------

#pragma once

#include "MyContainer.h"

[snip]

I think you'll find that things are better when you get rid of the
#include in MyClass. If you think about it, your compiler is telling
you that in MyContainer.h, MyClass doesn't exist - which is correct,
because of the point of the above #include, it hasn't been defined yet
:)

Jon.
 
N

Noah Roberts


Cyclic includes are always bad. The dependencies must be broken.

In reality you don't need to include either in either. It is always
better to minimize includes in headers...don't include something that
isn't needed...don't anticipate a need later...if there is a need try
and get rid of it.
 
A

axel22

make some correction in your code as follows:
MyClass.h
--------------------
#pragma once

class MyClass
{
public:
MyClass(void);
public:
~MyClass(void);
private:
int myInteger;
};

MyContainer.h
-------------------------
#pragma once

#include "MyClass.h"
#include <vector>

using namespace std;

class MyContainer
{
public:
MyContainer(void);
public:
~MyContainer(void);
private:
vector<MyClass *> myVector;
MyClass *pointer;
};

you dont need to include "MyContainer.h" in MyClass.h.


Neo: That's ok, but how can I make MyContainer a member of MyClass
then?

Jon: But how can I then create some sort of container which holds
pointers to the same class which has this container as a member? How
can I make a container which holds pointers to MyClass and is a member
of MyClass?

Dheeraj je napisao/la:
Add following to MyContainer.h

class MyClass; //forward declaration

Dheeraj

Dheeraj: Thnx, this solves the problem.

Victor Bazarov: Thnx. Adding forward declaration solves this.

Adding forward declaration in MyContainer solved the problem.
It was my intent to make a class MyClass which would hold a container
with pointers to another instances of MyClass. However, when I now try
to make MyContainer a member of MyClass like this:


---------------
MyClass.h
---------------

#pragma once

#include "MyContainer.h"

class MyClass {
private:
MyContainer memberContainer;
int myInteger;
public:
MyClass(void);
~MyClass(void);

};



I receive the following error:


Compiling...
MyContainer.cpp
f:\C++\Projects\Probarka\MyClass.h(7) : error C2146: syntax error :
missing ';' before identifier 'memberContainer'
f:\C++\Projects\Probarka\MyClass.h(7) : error C2501:
'MyClass::MyContainer' : missing storage-class or type specifiers
f:\C++\Projects\Probarka\MyClass.h(7) : error C2501:
'MyClass::memberContainer' : missing storage-class or type specifiers


Does anybody know why this doesn't work?
 
N

neo

forward declaration work like this

MyClass.h
-----------------

class MyClass;
#include "MyContainer.h"

regards,
-aims
 
N

neo

make some changes for forward declaration like this

MyContainer.h
----------------------
class MyContainer;
#include "MyClass.h"

MyClass.h
--------------------
class MyClass;
#include "MyContainer.h"

regards,
-aims
 
A

axel22

neo je napisao/la:
make some changes for forward declaration like this

MyContainer.h
----------------------
class MyContainer;
#include "MyClass.h"

MyClass.h
--------------------
class MyClass;
#include "MyContainer.h"

regards,
-aims

Thnx!
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top