Creating a class which holds a container with pointers to itself

A

axel22

Hello.

The idea is to create a class MyClass which holds a container
MyContainer with pointers to other instances of MyClass:

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

#pragma once

#include "MyContainer.h"

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

};



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

#pragma once

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

using namespace std;


class MyClass; // forward declaration

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;

}



This code results in the following error output:




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

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

Build log was saved at
"file://f:\C++\Projects\Probarka\Debug\BuildLog.htm"
Probarka - 3 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped



If anyone could help, I would be grateful.
 
H

Howard

Hello.

The idea is to create a class MyClass which holds a container
MyContainer with pointers to other instances of MyClass:

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

#pragma once

#include "MyContainer.h"

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

};



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

#pragma once

#include "MyClass.h"

Don't include MyClass.h here. All you need is the symbol MyClass, which
you're already forward-declaring below.
#include <vector>

using namespace std;


class MyClass; // forward declaration

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

};

-Howard
 
H

Howard

Also, don't do this in a header, ever. (And personally, I'd avoid doing it
anyplace.) Instead, you could just specify "std::vector" in your class
definition, instead of just "vector". (You could do "using std::vector;"
here, but it's easier in this case to just add "std::" to your vector member
declaration.)

-Howrad
 
N

Noah Roberts

Hello.

The idea is to create a class MyClass which holds a container

Why not just listen to the answers you got in the first thread you
posted about this and follow the advice found within?
 
A

axel22

Thnx, Howard!



Noah Roberts je napisao/la:
Why not just listen to the answers you got in the first thread you
posted about this and follow the advice found within?

"Don't include "MyClass.h" here." I saw it just now, other posters told
me to omit #include "MyContainer.h".
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top