Operator << not valid on unsigned?

S

Sensei

Hi.

I have a problem with a C++ code I can't resolve, or better, I can't see
what the problem should be!

Here's an excerpt of the incriminated code:

=== bspalgo.cpp

// THAT'S THE BAD FUNCTION!!
void save_bsp_tree( ofstream& of, bsptnodeptr p )
{
if( p->type == bsptnode::CUT ) {
of << (p->cut) << endl;
save_bsp_tree( of, p->below );
save_bsp_tree( of, p->above );
}
else {
if( p->type == bsptnode::IN )
of << "IN" << endl;
else if( p->type == bsptnode::OUT )
of << "OUT" << endl;
else
of << "!UNDEFINED!" << endl;
}
}

=== bsp.cpp

#include "bsp.h"

class bsptnode {
friend class bspt;
public:
enum Type {
CUT = 0x00,
...
};
enum {
UNDEFINED_CUT = unsigned(-1)
};

Type type;
unsigned cut; // <---- HERE'S THE BAD GUY!!
bsptnodeptr below;
bsptnodeptr above;
private:
...
};

=== bsp.h

#include "desf.h"

=== defs.h

typedef bsptnode* bsptnodeptr;


Now, if I compile this, I get:

boolalgo.cpp: In function `void save_bsp_tree(std::eek:fstream&, bsptnode*)':
boolalgo.cpp:67: error: no match for 'operator<<' in 'of <<
p->bsptnode::cut'
PlasmVector.h:143: note: candidates are: std::eek:stream&
operator<<(std::eek:stream&, const PlasmVector&)
matrix.h:96: note: std::eek:stream&
operator<<(std::eek:stream&, const matrix&)
PlasmMatrix.h:69: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmMatrix&)
PlasmID.h:52: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmID&)
PlasmHyperPlane.h:168: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmHyperPlane&)
....


Now, bsptnodeptr is a redefinition of a pointer, and the member in
question is an unsigned... I really can't see what's going wrong. I'm
using gcc 4 under macosx.
 
A

Alf P. Steinbach

* Sensei:
I have a problem with a C++ code I can't resolve, or better, I can't see
what the problem should be!

Here's an excerpt of the incriminated code:

=== bspalgo.cpp

// THAT'S THE BAD FUNCTION!!
void save_bsp_tree( ofstream& of, bsptnodeptr p )
{
if( p->type == bsptnode::CUT ) {
of << (p->cut) << endl;
Now, if I compile this, I get:

boolalgo.cpp: In function `void save_bsp_tree(std::eek:fstream&, bsptnode*)':
boolalgo.cpp:67: error: no match for 'operator<<' in 'of <<
p->bsptnode::cut'

Note that the filename don't match the filename you've given, and the types
suggested by the compiler don't match the types in the code you've given.
 
S

Sensei

Alf said:
Note that the filename don't match the filename you've given, and the types
suggested by the compiler don't match the types in the code you've given.

I was trying to make the names simple... Ok, I'm trying to port a
software on osx 10.4 (gcc 4). At first I had:

PlasmArray.h: In member function `void PlasmArray<T>::clear()':
PlasmArray.h:68: error: there are no arguments to 'begin' that depend on
a template parameter, so a declaration of 'begin' must be available
PlasmArray.h:68: error: (if you use `-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)
PlasmArray.h:68: error: there are no arguments to 'end' that depend on a
template parameter, so a declaration of 'end' must be available

Being PlasmArray:

template<class T>
class PlasmArray: public std::deque<T>
{
public:

so, in a function that used directly begin() and end(), I had to use
this->begin() and this->end() .... still wondering why.


Fixed that, in boolalgo.cpp if I include fstream, then I get:

/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc: In member function
`virtual typename std::basic_filebuf<_CharT, _Traits>::int_type
std::basic_filebuf<_CharT, _Traits>::underflow()':
/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc:277: error: expected
unqualified-id before '(' token
/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc: In member function
`virtual std::streamsize std::basic_filebuf<_CharT,
_Traits>::xsputn(const _CharT*, std::streamsize)':
/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc:585: error: expected
unqualified-id before '(' token
make[2]: *** [boolalgo.o] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

The only way to fix it, and I don't know why, is to delete the directive
#include <fstream>. Then I have:

=== boolalgo.cpp

#include "bsp.h"
//#include <fstream>
#include <queue>
#include <stack>

using namespace std;
....
void save_bsp_tree( ofstream& of, bsptnodeptr p )
{
if( p->type == bsptnode::CUT ) {
of << (p->cut) << endl;
save_bsp_tree( of, p->below );
save_bsp_tree( of, p->above );
}
else {
if( p->type == bsptnode::IN )
of << "IN" << endl;
else if( p->type == bsptnode::OUT )
of << "OUT" << endl;
else
of << "!UNDEFINED!" << endl;
}
}

=== bsp.h

#ifndef __BSP_H
#define __BSP_H
#ifdef WIN32
#pragma warning(disable:4786)
#endif
#include "GeneralDef.h"


class bsptnode {
friend class bspt;

public:
enum Type {
CUT = 0x00,
...
};
enum {
UNDEFINED_CUT = unsigned(-1)
};
Type type;
unsigned cut;
bsptnodeptr below;
bsptnodeptr above;
private:
bsptnode( const bsptnode& );
bsptnode& operator=( const bsptnode& );
public:
bsptnode():type(OUT),cut(UNDEFINED_CUT),below(0),above(0) {}
bsptnode(Type t):type(t),cut(UNDEFINED_CUT),below(0),above(0) {}
bsptnode(Type t, unsigned c):type(t),cut(c),below(0),above(0) {}

~bsptnode() {
if (above)
delete above;
if (below)
delete below;
}
};

=== GeneralDef.h

#ifndef _GENERAL_DEFS_H__
#define _GENERAL_DEFS_H__
#ifdef WIN32
#pragma warning (disable:4786)
#endif
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <math.h>
#include <assert.h>

class bspt;
class bsptnode;
class hyperplanes;
class ISystem;
class equation;

typedef bspt* bsptptr;

typedef bsptnode* bsptnodeptr;

===END===

Compiling it:

boolalgo.cpp: In function `void save_bsp_tree(std::eek:fstream&, bsptnode*)':
boolalgo.cpp:70: error: no match for 'operator<<' in 'of <<
p->bsptnode::cut'
PlasmVector.h:143: note: candidates are: std::eek:stream&
operator<<(std::eek:stream&, const PlasmVector&)
matrix.h:96: note: std::eek:stream&
operator<<(std::eek:stream&, const matrix&)
PlasmMatrix.h:69: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmMatrix&)
PlasmID.h:52: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmID&)
....
PlasmArray.h:136: note: std::basic_ostream<char,
std::char_traits<char> >& operator<<(std::basic_ostream<char,
std::char_traits<char> >&, const PlasmArray<PlasmHyperPlane>&)
GeneralDef.h:184: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmSet&)
PlasmFastArray.h:248: note: std::basic_ostream<char,
std::char_traits<char> >& operator<<(std::basic_ostream<char,
std::char_traits<char> >&, const PlasmFastArray<PlasmIndex>&)
bsp.h:95: note: std::eek:stream& operator<<(std::eek:stream&,
const equation&)
bsp.h:228: note: std::eek:stream& operator<<(std::eek:stream&,
bspt&)
bsp.h:251: note: std::eek:stream& operator<<(std::eek:stream&,
ISystemData&)
bsp.h:303: note: std::eek:stream& operator<<(std::eek:stream&,
ISystem&)
....
GeneralDef.h:184: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmSet&)
PlasmFastArray.h:248: note: std::basic_ostream<char,
std::char_traits<char> >& operator<<(std::basic_ostream<char,
std::char_traits<char> >&, const PlasmFastArray<PlasmIndex>&)
bsp.h:95: note: std::eek:stream& operator<<(std::eek:stream&,
const equation&)
bsp.h:228: note: std::eek:stream& operator<<(std::eek:stream&,
bspt&)
bsp.h:251: note: std::eek:stream& operator<<(std::eek:stream&,
ISystemData&)
bsp.h:303: note: std::eek:stream& operator<<(std::eek:stream&,
ISystem&)

***continuing reading I find also:

boolalgo.cpp:78: error: no match for 'operator<<' in 'of << "OUT"'
PlasmVector.h:143: note: candidates are: std::eek:stream&
operator<<(std::eek:stream&, const PlasmVector&)
matrix.h:96: note: std::eek:stream&
operator<<(std::eek:stream&, const matrix&)
PlasmMatrix.h:69: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmMatrix&)
....
boolalgo.cpp:80: error: no match for 'operator<<' in 'of << "!UNDEFINED!"'
PlasmVector.h:143: note: candidates are: std::eek:stream&
operator<<(std::eek:stream&, const PlasmVector&)
....

***and at last:

boolalgo.cpp: In function `void save_bsp(char*, bsptnode*, hyperplanes)':
boolalgo.cpp:176: error: variable 'std::eek:fstream of' has initializer but
incomplete type
make[2]: *** [boolalgo.o] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
sensei:plasm$
 
A

Alf P. Steinbach

* Sensei:
I was trying to make the names simple... Ok, I'm trying to port a
software on osx 10.4 (gcc 4). At first I had:

PlasmArray.h: In member function `void PlasmArray<T>::clear()':
PlasmArray.h:68: error: there are no arguments to 'begin' that depend on
a template parameter, so a declaration of 'begin' must be available
PlasmArray.h:68: error: (if you use `-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)
PlasmArray.h:68: error: there are no arguments to 'end' that depend on a
template parameter, so a declaration of 'end' must be available

Being PlasmArray:

template<class T>
class PlasmArray: public std::deque<T>
{
public:

so, in a function that used directly begin() and end(), I had to use
this->begin() and this->end() .... still wondering why.

The class inherited from is not a concrete class but a template. Whether
it has being() and end() members depends in principle on the type T. So
you have to tell the compiler that those are inherited members.

An alternative to qualification with "this->" is to declare the members via
'using':

using std::deque<T>::begin();
using std::deque<T>::end();

What you don't say is where the heck this PlasmArray is located and how the
compiler knows about it: it's not referenced in the code you present.

In other words:

* You're not providing the RELEVANT code, and

* It seems like at least some #include directive is omitted in the
code you present, as compared to the actual code.


Fixed that, in boolalgo.cpp if I include fstream, then I get:

/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc: In member function
`virtual typename std::basic_filebuf<_CharT, _Traits>::int_type
std::basic_filebuf<_CharT, _Traits>::underflow()':
/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc:277: error: expected
unqualified-id before '(' token
/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc: In member function
`virtual std::streamsize std::basic_filebuf<_CharT,
_Traits>::xsputn(const _CharT*, std::streamsize)':
/usr/include/gcc/darwin/4.0/c++/bits/fstream.tcc:585: error: expected
unqualified-id before '(' token
make[2]: *** [boolalgo.o] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

The only way to fix it, and I don't know why, is to delete the directive
#include <fstream>.

That's certainly not a fix, it's hiding the error behind some other error
or arbitrary effect.

The errors indicate a missing semicolon at the end of a class definition,
but it could really be anything.

Without the code it's difficult to say.

Then I have:

=== boolalgo.cpp

#include "bsp.h"
//#include <fstream>
#include <queue>
#include <stack>

using namespace std;
...
void save_bsp_tree( ofstream& of, bsptnodeptr p )
{
if( p->type == bsptnode::CUT ) {
of << (p->cut) << endl;
save_bsp_tree( of, p->below );
save_bsp_tree( of, p->above );
}
else {
if( p->type == bsptnode::IN )
of << "IN" << endl;
else if( p->type == bsptnode::OUT )
of << "OUT" << endl;
else
of << "!UNDEFINED!" << endl;
}
}

=== bsp.h

#ifndef __BSP_H
#define __BSP_H
#ifdef WIN32
#pragma warning(disable:4786)
#endif
#include "GeneralDef.h"


class bsptnode {
friend class bspt;

public:
enum Type {
CUT = 0x00,
...
};
enum {
UNDEFINED_CUT = unsigned(-1)
};
Type type;
unsigned cut;
bsptnodeptr below;
bsptnodeptr above;
private:
bsptnode( const bsptnode& );
bsptnode& operator=( const bsptnode& );
public:
bsptnode():type(OUT),cut(UNDEFINED_CUT),below(0),above(0) {}
bsptnode(Type t):type(t),cut(UNDEFINED_CUT),below(0),above(0) {}
bsptnode(Type t, unsigned c):type(t),cut(c),below(0),above(0) {}

~bsptnode() {
if (above)
delete above;
if (below)
delete below;
}
};

=== GeneralDef.h

#ifndef _GENERAL_DEFS_H__
#define _GENERAL_DEFS_H__
#ifdef WIN32
#pragma warning (disable:4786)
#endif
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <math.h>
#include <assert.h>

class bspt;
class bsptnode;
class hyperplanes;
class ISystem;
class equation;

typedef bspt* bsptptr;

typedef bsptnode* bsptnodeptr;

===END===

Compiling it:

boolalgo.cpp: In function `void save_bsp_tree(std::eek:fstream&, bsptnode*)':
boolalgo.cpp:70: error: no match for 'operator<<' in 'of <<
p->bsptnode::cut'
PlasmVector.h:143: note: candidates are: std::eek:stream&
operator<<(std::eek:stream&, const PlasmVector&)
matrix.h:96: note: std::eek:stream&
operator<<(std::eek:stream&, const matrix&)
PlasmMatrix.h:69: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmMatrix&)
PlasmID.h:52: note: std::eek:stream&
operator<<(std::eek:stream&, const PlasmID&)

Presumably this would be fixed by including <fstream> in [boolalgo.cpp].
 

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
474,204
Messages
2,571,064
Members
47,672
Latest member
svaraho

Latest Threads

Top