anonymous namespaces and scope resolution question

2

2b|!2b==?

I have the following declaration in a header file (MyClass.h)

//contents of MyClass.h
class MyClass
{
public:
MyClass()
MyClass(const MyClass&);
...
};
typedef std::vector<MyClass> MyClassVect;
typedef MyClassVect::iterator MyClassVectIter;
typedef MyClassVect::const_iterator MyClassVectConstIter;



//Contents of souce file (AnotherFile.cpp)
#include "MyClass.h"

namespace
{
MyClassVector classes ;
classes.push_back(new MyClass()); //syntax error : missing ';' before '.'
.......
}

The type of variable 'classes' is not recognized as a vector (as
specified in the declaration), but rather as an int (i.e. int
'anonymous-namespace'::classes).

question is - how may I use a variable of the types declared in
"MyClass.h" ?
 
S

Serge Skorokhodov

2b|!2b==? said:
I have the following declaration in a header file (MyClass.h)

//contents of MyClass.h
class MyClass
{
public:
MyClass()
MyClass(const MyClass&);
..
};
typedef std::vector<MyClass> MyClassVect;
typedef MyClassVect::iterator MyClassVectIter;
typedef MyClassVect::const_iterator MyClassVectConstIter;



//Contents of souce file (AnotherFile.cpp)
#include "MyClass.h"

namespace
{
MyClassVector classes ;
classes.push_back(new MyClass()); //syntax error : missing ';' before '.'
.......
}

I'm afraid you can place only declarations inside a namespace (whether
anonymous or not).
 
A

anon

2b|!2b==? said:
I have the following declaration in a header file (MyClass.h)

//contents of MyClass.h
class MyClass
{
public:
MyClass()
MyClass(const MyClass&);
..
};
typedef std::vector<MyClass> MyClassVect;
typedef MyClassVect::iterator MyClassVectIter;
typedef MyClassVect::const_iterator MyClassVectConstIter;



//Contents of souce file (AnotherFile.cpp)
#include "MyClass.h"

namespace
{
MyClassVector classes ;

Can you mark the line where you declared/defined MyClassVector?
 
2

2b|!2b==?

anon said:
Can you mark the line where you declared/defined MyClassVector?

That was a typo. MyClassVector should read MyClassVect in the source
file "AnotherFile.cpp"
 
A

anon

2b|!2b==? said:
That was a typo. MyClassVector should read MyClassVect in the source
file "AnotherFile.cpp"

Sorry, but I do not see how MyClassVector reads MyClassVect, and your
example does not compile to show that.

Maybe someone else see whats going on there
 
S

SMHealthNick

Sorry, but I do not see how MyClassVector reads MyClassVect, and your
example does not compile to show that.

Maybe someone else see whats going on there- Hide quoted text -

- Show quoted text -

good point
 
J

Jim Langston

2b|!2b==? said:
I have the following declaration in a header file (MyClass.h)

//contents of MyClass.h
class MyClass
{
public:
MyClass()
MyClass(const MyClass&);
..
};
typedef std::vector<MyClass> MyClassVect;
typedef MyClassVect::iterator MyClassVectIter;
typedef MyClassVect::const_iterator MyClassVectConstIter;



//Contents of souce file (AnotherFile.cpp)
#include "MyClass.h"

namespace
{
MyClassVector classes ;
classes.push_back(new MyClass()); //syntax error : missing ';' before
'.'
.......
}

The type of variable 'classes' is not recognized as a vector (as specified
in the declaration), but rather as an int (i.e. int
'anonymous-namespace'::classes).

question is - how may I use a variable of the types declared in
"MyClass.h" ?

Too many problems with this code to say what is going on as others have
pointed out. Please provide compilable (even with the error) code that
demonstrates the problem.
1. MyClassVect != MyClassVector
2. classes.push_back... executable code outside a function body.
3. no main.
 

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,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top