class definition - namespace nomenclature clarification

K

Karthik Kumar

Hi,
I was writing this application that used namespaces.
The structure of the file was as follows.


//Header file


#ifndef MYNS_MYHEADER_H
#define MYNS_MYHEADER_H

namespace MyNS {

class MyClass {
//Declare methods and fields.
int DoSomething(int , int );

};
}


#endif



// Source file

#include "myns_myheader.h"

using MyNS::MyClass;

int MyClass::DoSomething(int a, int b) {
return a + b;
}



With GCC 3.3.1 - This code compiles correctly.
With MS VC .NET compiler - the compiler errors out .

error C2511: 'void MyNS::MyClass::DoSomething(const int)' : overloaded
member function not found in 'Geometry::TrackBall'
c:\temp\myns_myheader.h(8) : see declaration of 'MyNS::MyClass'


When I changed the C++ source code (with the fully-qualified name of the
class as follows:

//no 'using' statement here

int MyNS::MyClass::DoSomething(int a, int b) {
return a + b;
}


Then it compiled successfully.

* What does the C++ language standard specify regarding this usage ?
Who is correct - GCC / VS .NET ? Or, does it depend on the
implementation ?

Thanks for clarifying this.
 
S

Sharad Kala

Karthik Kumar said:
Hi,
I was writing this application that used namespaces.
The structure of the file was as follows.


//Header file


#ifndef MYNS_MYHEADER_H
#define MYNS_MYHEADER_H

namespace MyNS {

class MyClass {
//Declare methods and fields.
int DoSomething(int , int );

};
}


#endif



// Source file

#include "myns_myheader.h"

using MyNS::MyClass;

int MyClass::DoSomething(int a, int b) {
return a + b;
}



With GCC 3.3.1 - This code compiles correctly.
With MS VC .NET compiler - the compiler errors out .

error C2511: 'void MyNS::MyClass::DoSomething(const int)' : overloaded
member function not found in 'Geometry::TrackBall'
c:\temp\myns_myheader.h(8) : see declaration of 'MyNS::MyClass'

The error seems to be with your not providing an overload of DoSomething
taking one int as parameter. You can see that the compiler is deducing the
context as MyNS::MyClass::DoSomething in the error message.

When I changed the C++ source code (with the fully-qualified name of the
class as follows:

//no 'using' statement here

int MyNS::MyClass::DoSomething(int a, int b) {
return a + b;
}

I think the error message lies somewhere else.

Sharad
 
H

Howard

Karthik Kumar said:
Hi,
I was writing this application that used namespaces.
The structure of the file was as follows.


//Header file


#ifndef MYNS_MYHEADER_H
#define MYNS_MYHEADER_H

namespace MyNS {

class MyClass {
//Declare methods and fields.
int DoSomething(int , int );

This function takes two int parameters. But see below...
};
}


#endif



// Source file

#include "myns_myheader.h"

using MyNS::MyClass;

int MyClass::DoSomething(int a, int b) {
return a + b;
}



With GCC 3.3.1 - This code compiles correctly.
With MS VC .NET compiler - the compiler errors out .

error C2511: 'void MyNS::MyClass::DoSomething(const int)' : overloaded
member function not found in 'Geometry::TrackBall'
c:\temp\myns_myheader.h(8) : see declaration of 'MyNS::MyClass'

This is complaining about not finding a DoSomething that takes a single
const int parameter. Look in the function TrackBall, as stated in the error
message. What are you calling there?
When I changed the C++ source code (with the fully-qualified name of the
class as follows:

//no 'using' statement here

int MyNS::MyClass::DoSomething(int a, int b) {
return a + b;
}


Then it compiled successfully.

Without seeing the call in Geometry::TrackBall, who can tell? Is Geometry
in another namespace? I think that there are issues of "hiding" functions
called from one namespace to another in VS, which may be resolved by
specifying the namespace. But this isn't enough code for me to duplicate
the problem here.
* What does the C++ language standard specify regarding this usage ?

What usage? Calling a function in a namespace? There is nothing in what
you've shown that in itself exhibits a problem. There must be a problem
caused by something in the code you haven't shown.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top