Namespace problem 
Author Message
 Namespace problem

I have an anoying problem with a namespace declaration. The code snippet
below is created to isolate the problem and should not be taken for more
than it is.

When I compile this code with Visual C++ 5.0 sp3 I get the following
errors:

D:\Program\DevStudio\VC\INCLUDE\vector(107) : error C2065: 'CObject' :
undeclared identifier
D:\Program\DevStudio\VC\INCLUDE\vector(107) : error C2440: 'default
argument' : cannot convert from 'int' to 'const class test::CObject &'
                                                           Reason:
cannot convert from 'int' to 'const class test::CObject'
                                                           No
constructor could take the source type, or constructor overload
resolution was ambiguous

Does anyone have any suggestion?

Tobias Gabrielsson

Code snippet follows:
--------------------------------
// Header files
#include <vector>
#include <utility>

namespace test
{

class CObject
{
public:
    double   dValue;

public:
    CObject( void )
        : dValue( 0.0 )
    {}
    CObject( const CObject &rhs )
        : dValue( rhs.dValue )
    {}

    operator==( CObject &rhs )    { return dValue == rhs.dValue; }
    operator<( CObject &rhs )    { return dValue < rhs.dValue; }
    operator==( const CObject &rhs ) const  { return dValue ==
rhs.dValue; }
    operator<( const CObject &rhs )  const  { return dValue <
rhs.dValue; }

Quote:
};

class CContainer
{
public:
    std::vector<CObject>  m_vDepthArea;

Quote:
};
}   // End of namespace test

int main( void )
{
    test::CContainer obj;

    return 0;

Quote:
}



Fri, 20 Apr 2001 03:00:00 GMT  
 Namespace problem
Hi

I have version 6.0 and it also does'nt handle namespaces very well.
The following code does not compile. It reports i is private and
cannot be accessed. The error is
error C2248: 'i' : cannot access private member declared in class 'A'

If I move i to public then the error is
error C2593: 'operator <<' is ambiguous

Everything works fine without namespace std.

Why should microsoft ship a product with such obvious bugs ?

#include <iostream>
using namespace std;
class A {
        int i;
public:
        A() { i =5;}
        friend ostream& operator << (ostream & o, const A & a);

Quote:
};

ostream& operator << (ostream & o, const A & a)
{
        o << "Printing A = " << a.i << endl;
        return o;

Quote:
}

int main()
{
        A a;
        cout << a;
        return 0;

Quote:
}

Dhaval Sheth
pls edit the reply to address before replying.
Quote:

> I have an anoying problem with a namespace declaration. The code
> snippet below is created to isolate the problem and should not be
> taken for more than it is.

> When I compile this code with Visual C++ 5.0 sp3 I get the following
> errors:

> D:\Program\DevStudio\VC\INCLUDE\vector(107) : error C2065: 'CObject' :
> undeclared identifier
> D:\Program\DevStudio\VC\INCLUDE\vector(107) : error C2440: 'default
> argument' : cannot convert from 'int' to 'const class test::CObject &'

>                                                            Reason:
> cannot convert from 'int' to 'const class test::CObject'
>                                                            No
> constructor could take the source type, or constructor overload
> resolution was ambiguous

> Does anyone have any suggestion?

> Tobias Gabrielsson

> Code snippet follows:
> --------------------------------
> // Header files
> #include <vector>
> #include <utility>

> namespace test
> {

> class CObject
> {
> public:
>     double   dValue;

> public:
>     CObject( void )
>         : dValue( 0.0 )
>     {}
>     CObject( const CObject &rhs )
>         : dValue( rhs.dValue )
>     {}

>     operator==( CObject &rhs )    { return dValue == rhs.dValue; }
>     operator<( CObject &rhs )    { return dValue < rhs.dValue; }
>     operator==( const CObject &rhs ) const  { return dValue ==
> rhs.dValue; }
>     operator<( const CObject &rhs )  const  { return dValue <
> rhs.dValue; }
> };

> class CContainer
> {
> public:
>     std::vector<CObject>  m_vDepthArea;
> };

> }   // End of namespace test

> int main( void )
> {
>     test::CContainer obj;

>     return 0;
> }



Tue, 24 Apr 2001 03:00:00 GMT  
 Namespace problem
hmmm, compiled fine under 5, i public or private
Quote:

> Hi

> I have version 6.0 and it also does'nt handle namespaces very well.
> The following code does not compile. It reports i is private and
> cannot be accessed. The error is
> error C2248: 'i' : cannot access private member declared in class 'A'

> If I move i to public then the error is
> error C2593: 'operator <<' is ambiguous

> Everything works fine without namespace std.

> Why should microsoft ship a product with such obvious bugs ?

> #include <iostream>
> using namespace std;
> class A {
>         int i;
> public:
>         A() { i =5;}
>         friend ostream& operator << (ostream & o, const A & a);
> };

> ostream& operator << (ostream & o, const A & a)
> {
>         o << "Printing A = " << a.i << endl;
>         return o;
> }

> int main()
> {
>         A a;
>         cout << a;
>         return 0;
> }

> Dhaval Sheth



Thu, 26 Apr 2001 03:00:00 GMT  
 Namespace problem
I have VC5 sp3 and it worked fine for me

Quote:

> When I compile this code with Visual C++ 5.0 sp3 I get the following
> errors:

...


Thu, 26 Apr 2001 03:00:00 GMT  
 Namespace problem
DS>I have version 6.0 and it also does'nt handle namespaces very well.
DS>The following code does not compile. It reports i is private and
DS>cannot be accessed. The error is
DS>error C2248: 'i' : cannot access private member declared in class 'A'
DS>
DS>If I move i to public then the error is
DS>error C2593: 'operator <<' is ambiguous
DS>
DS>Everything works fine without namespace std.
DS>
[snip]
DS>
DS>#include <iostream>
DS>using namespace std;
DS>class A {
DS>  int i;
DS>public:
DS>  A() { i =5;}
DS>  friend ostream& operator << (ostream & o, const A & a);
DS>};
DS>
DS>ostream& operator << (ostream & o, const A & a)
DS>{
DS>  o << "Printing A = " << a.i << endl;
DS>  return o;
DS>}
DS>
DS>int main()
DS>{
DS>  A a;
DS>  cout << a;
DS>  return 0;
DS>}

        I too have had some minor problems with namespaces, friendship,
etc. The problems seem to have focussed on global and class static
functions. I've been trying to determine if maybe what I've thought was
a problem might actually be a feature. I can see some advantage with
being very specific when it comes to granting friendship -- for
instance. As a rule, I usually only include "using namespace" directives
in .cpp files. Interestingly, I can alter your code slightly and get it
to go fine. For example:

        // Typically in header.
        #include <iostream>

        class A {
                int i;
        public:
                A() { i =5;}
                friend std::ostream& operator << (
                        std::ostream & o, const A & a);
        };

        // Typically in source including header.
        using namespace std;

        ostream& operator << (ostream & o, const A & a)
        {
                o << "Printing A = " << a.i << endl;
                return o;
        }

        int main()
        {
                A a;
                cout << a;
                return 0;
        }

If I move the using directive in front of the class, then I run into
problems even with the explicit friendship declaration. So, the problem
seems to be with an active using directive in place at the time a class
is declared. Were it not for this last case, I'd say that we were
definitely looking at a feature. However, ....

Best of Luck,
Wes



Fri, 27 Apr 2001 03:00:00 GMT  
 Namespace problem
Try this:
#include <iostream.h>

namespace std {
using namespace std;

Quote:
}

class A
{
int i;
public:
A() { i =5;}
friend ostream& operator << (ostream & o, const A & a);

Quote:
};

ostream& operator << (ostream & o, const A & a)
{
o << "Printing A = " << a.i << endl;
return o;

Quote:
}

int main()
{
A a;
cout << a;
return 0;

Quote:
}

Also see:Compiler Error C2871
'name' : does not exist or is not a namespace

You did one of the following:

You tried to extend an item that is not a namespace.

You tried to add a namespace with a using declaration that does not exist.

You tried to add a namespace with a using declaration that is not a
namespace.
The following code shows all three errors:

int A;
int i;

namespace A { \\ error: cannot extend; A is not a namespace
   using namespace B; \\ error: namespace B does not exist
   using namespace i; \\ error: i is not a namespace

Quote:
}
-----Original Message-----

>Hi

>I have version 6.0 and it also does'nt handle namespaces very well.
>The following code does not compile. It reports i is private and
>cannot be accessed. The error is
>error C2248: 'i' : cannot access private member declared in class 'A'

>If I move i to public then the error is
>error C2593: 'operator <<' is ambiguous

>Everything works fine without namespace std.

>Why should microsoft ship a product with such obvious bugs ?

>#include <iostream>
>using namespace std;
>class A {
> int i;
>public:
> A() { i =5;}
> friend ostream& operator << (ostream & o, const A & a);
>};

>ostream& operator << (ostream & o, const A & a)
>{
> o << "Printing A = " << a.i << endl;
> return o;
>}

>int main()
>{
> A a;
> cout << a;
> return 0;
>}

>Dhaval Sheth
>pls edit the reply to address before replying.


>> I have an anoying problem with a namespace declaration. The code
>> snippet below is created to isolate the problem and should not be
>> taken for more than it is.

>> When I compile this code with Visual C++ 5.0 sp3 I get the following
>> errors:

>> D:\Program\DevStudio\VC\INCLUDE\vector(107) : error C2065: 'CObject' :
>> undeclared identifier
>> D:\Program\DevStudio\VC\INCLUDE\vector(107) : error C2440: 'default
>> argument' : cannot convert from 'int' to 'const class test::CObject &'

>>                                                            Reason:
>> cannot convert from 'int' to 'const class test::CObject'
>>                                                            No
>> constructor could take the source type, or constructor overload
>> resolution was ambiguous

>> Does anyone have any suggestion?

>> Tobias Gabrielsson

>> Code snippet follows:
>> --------------------------------
>> // Header files
>> #include <vector>
>> #include <utility>

>> namespace test
>> {

>> class CObject
>> {
>> public:
>>     double   dValue;

>> public:
>>     CObject( void )
>>         : dValue( 0.0 )
>>     {}
>>     CObject( const CObject &rhs )
>>         : dValue( rhs.dValue )
>>     {}

>>     operator==( CObject &rhs )    { return dValue == rhs.dValue; }
>>     operator<( CObject &rhs )    { return dValue < rhs.dValue; }
>>     operator==( const CObject &rhs ) const  { return dValue ==
>> rhs.dValue; }
>>     operator<( const CObject &rhs )  const  { return dValue <
>> rhs.dValue; }
>> };

>> class CContainer
>> {
>> public:
>>     std::vector<CObject>  m_vDepthArea;
>> };

>> }   // End of namespace test

>> int main( void )
>> {
>>     test::CContainer obj;

>>     return 0;
>> }



Sun, 06 May 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Namespace problem

2. browse symbols + namespaces = problem

3. XML namespace problem

4. C++ Namespace Problem with VC++6.

5. CrystalDecisions namespace problem building from command line.

6. System.WinForms Namespace problem....

7. Windows namespace problem

8. Namespace problem

9. MIDL namespace problems using importlib

10. namespace problems

11. Namespace problem???

12. Standard Namespace problem

 

 
Powered by phpBB® Forum Software