istream illegal operands

frac707

Registered
I'm trying to port some code from Windows to Macintosh using
Codewarrior9 and Carbon. For the following code:

inline istream& operator>>(istream& in, Vec3& v)
{
return in >> "[" >> v[0] >> v[1] >> v[2] >> "]";
}

I receive this error message:

Error : illegal operands 'std::basic_istream<char, std::char_traits<char>>' >> 'const char *'
Vec3.h line 216 return in >> "[" >> v[0] >> v[1] >> v[2] >> "]";

with the second '>>' highlighted

Similar inline code for ostream gives no errors:
inline ostream& operator<<(ostream& out, const Vec4& v)
{
return
out << "[" << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << "]";
}

The code is part of a c++ 3d vector library. I'm not very familiar with the std stream libraries, but this looks like standard ANSI code to me, so why does Codewarrior balk on it?

Thanks for any help on this,

Terry
 
Think of ostream as some form of printf, and istream as a kind of scanf. Istream is basically storing data into the arguments on the right. So:

in >> "[" >> v[0] >> v[1] >> v[2] >> "]";

is not going to work, since "[" is a const char * and so is "]". Removing those will work, but I'm assuming it'll break the formatting of your text file. So here's how I would fix it. Declare two new variables of type char (or string) and replace the occurrences of "[" and "]" with the new variables you've declared.
 
Thanks! That helped.

I inserted some char strings like you suggested, and all the cpp files now compile, but I get a lot of warnings like

Warning : inline function call 'Vec4::Vec4(const Vec3 &, double)' not inlined
qslim.cpp line 76 }

Also the entry function to the cpp files won't link to the rest of the program. Is it alright to mix c source with cpp source in the same CodeWarrior project? The PPC linker in this case can't find a function in a .cpp file that is referenced by a .c file, though it works in reverse: the cpp file does link to a function in the .c file, like you'd expect. The functions are prototyped the same in both files. Do I need to compile everything as c++???

Terry
 
I have no experience with Metrowerk's compiler and all my experience is with gcc. However, assuming that all compilers are more or less equal (hah! yeah right, but humor me ;)) C files can be compiled by C++ compilers since C is a subset of C++ but C++ files can't be compiled by C compilers, since there are many constructs in C++ that are completely foreign to C.

I suggest compiling in C++. You might need to change some declarations, but it's generally going to be less painful than compiling C++ code with a C compiler. Don't worry too much about the warnings. I don't think they are harmful.
 
Thanks again! I recompiled everything as c++ and it wasn't as difficult as I thought. The program links and it didn't seem to break anything, but there is a problem in the vector code -- crashes the program. To debug this I really need a working debugger -- sigh! Metrowerks' customer support stinks. It's been three days and I still can't login to their site.

Terry
 
Are you running Panther? If you are you could try downloading the free XCode developer tools and compile using gcc. I don't know how gcc compares with the Metrowerks compiler, but it's a very popular compiler and I imagine it would be more standards compliant than Metrowerks.
 
Back
Top