The compiler do not recognize my function (newbie).

S

sphenxes

#include <iostream>
using namespace std;

class CatFive
{
private:
int itsAge;

public:
void hi(); //source of error
CatFive :: CatFive(int cage);
~CatFive();
void setData(int age);
int getData();
};

//////////////////////////

CatFive :: CatFive(int cage)
{
itsAge = cage;
}

CatFive :: ~CatFive()
{
}

void CatFive :: setData(int age)
{
itsAge = age;
}

int CatFive :: getData()
{
return itsAge;
}

void CatFive :: hi() //source of error
{
cout << " hi and welcome" << endl;
}

////////////////////////////////////
int main()
{
CatFive Frisky(1);

cout << " Frisky is " << Frisky.getData() << "years old" << endl;

Frisky.setData(3);
Frisky.getData(),

cout << Frisky.hi(); //source of error
cout << "Frisky is now " << Frisky.getData()<< "years old" << endl;

return 0;
}


Hi
I try to compile this program but I get a lot of errors because of
void hi(); function,
if I delete this function and all related data, the program is compiled
without any problems.
I am using g++ as a compiler (version 3.3.3) on linux Suse 9.1.
I have compiled similar programs several times before. I can't find the
error on my own.
Any sugession is appreciated.

Regards
Hesham Marei (sphenxes)








Compiler Errors
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `getData' undeclared (first use this function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `FriskyFrisky' undeclared (first use this function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc: In function `int main()':
CatFive.cc:53: error: no match for 'operator<<' in 'std::cout <<
(&Frisky)->CatFive::hi()'
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:74: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits
= std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:86: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
 
A

adbarnet

It appears you are not 'using' namespace std on you cout and endl's try
std::cout and std::endl - or using namespace statement (not recomended if
you are including the cc file with your header).

ad

sphenxes said:
#include <iostream>
using namespace std;

class CatFive
{
private:
int itsAge;

public:
void hi(); //source of error
CatFive :: CatFive(int cage);
~CatFive();
void setData(int age);
int getData();
};

//////////////////////////

CatFive :: CatFive(int cage)
{
itsAge = cage;
}

CatFive :: ~CatFive()
{
}

void CatFive :: setData(int age)
{
itsAge = age;
}

int CatFive :: getData()
{
return itsAge;
}

void CatFive :: hi() //source of error
{
cout << " hi and welcome" << endl;
}

////////////////////////////////////
int main()
{
CatFive Frisky(1);

cout << " Frisky is " << Frisky.getData() << "years old" << endl;

Frisky.setData(3);
Frisky.getData(),

cout << Frisky.hi(); //source of error
cout << "Frisky is now " << Frisky.getData()<< "years old" << endl;

return 0;
}


Hi
I try to compile this program but I get a lot of errors because of
void hi(); function,
if I delete this function and all related data, the program is compiled
without any problems.
I am using g++ as a compiler (version 3.3.3) on linux Suse 9.1.
I have compiled similar programs several times before. I can't find the
error on my own.
Any sugession is appreciated.

Regards
Hesham Marei (sphenxes)








Compiler Errors
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `getData' undeclared (first use this function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `FriskyFrisky' undeclared (first use this function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc: In function `int main()':
CatFive.cc:53: error: no match for 'operator<<' in 'std::cout <<
(&Frisky)->CatFive::hi()'
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:74: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits
= std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:86: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
 
R

Raymond Martineau

#include <iostream>
using namespace std;

class CatFive
{
public:
void hi(); //source of error

This function returns void.
void CatFive :: hi() //source of error
{
cout << " hi and welcome" << endl;
}
int main()
{

Frisky.getData(),

Switch the comma with a semi-colon.
cout << Frisky.hi(); //source of error

Right here, you are trying to print the return value of the Frisky.hi(),
which is of type void. Cout does not have an operator that does this -
either change the return value of Frisky.hi(), or avoid passing it to cout.
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc: In function `int main()':
CatFive.cc:53: error: no match for 'operator<<' in 'std::cout <<
(&Frisky)->CatFive::hi()'

The answer is clearly stated right here. The other lines that follow
describe the available functions that can be used.
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:74: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits
= std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:86: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
 
R

Rade

cout << Frisky.hi(); //source of error

Your function hi has no return value, so its return value cannot be printed
on cout. Instead, just call:

Frisky.hi();

Regards,
Rade
 
S

sphenxes

Hi ad

Thank you for you reply and help. I still have few questions because I do
not really understand what is going on.
First:
I am using this example from Teach yourself c++ in 21 days. I tried to use
#include <iostream.h> which of course have not work. Then I tried to use
#include <iostram> and using namespace std; which worked with all my
previous programs (it is clear that I am not quit understanding what I am
doing).
You have just found the mistake and it has something to do with namespace.
My question:
where should I use namespace. I have already use it on the second line of my
program which of course seems not to work,
Would you please elaborate on how and where should I use namespace.

Thank you once more for your help.

Regards
Hesham Marei
sphenxes
It appears you are not 'using' namespace std on you cout and endl's try
std::cout and std::endl - or using namespace statement (not recomended if
you are including the cc file with your header).

ad

sphenxes said:
#include <iostream>
using namespace std;

class CatFive
{
private:
int itsAge;

public:
void hi(); //source of error
CatFive :: CatFive(int cage);
~CatFive();
void setData(int age);
int getData();
};

//////////////////////////

CatFive :: CatFive(int cage)
{
itsAge = cage;
}

CatFive :: ~CatFive()
{
}

void CatFive :: setData(int age)
{
itsAge = age;
}

int CatFive :: getData()
{
return itsAge;
}

void CatFive :: hi() //source of error
{
cout << " hi and welcome" << endl;
}

////////////////////////////////////
int main()
{
CatFive Frisky(1);

cout << " Frisky is " << Frisky.getData() << "years old" << endl;

Frisky.setData(3);
Frisky.getData(),

cout << Frisky.hi(); //source of error
cout << "Frisky is now " << Frisky.getData()<< "years old" << endl;

return 0;
}


Hi
I try to compile this program but I get a lot of errors because of
void hi(); function,
if I delete this function and all related data, the program is compiled
without any problems.
I am using g++ as a compiler (version 3.3.3) on linux Suse 9.1.
I have compiled similar programs several times before. I can't find the
error on my own.
Any sugession is appreciated.

Regards
Hesham Marei (sphenxes)








Compiler Errors
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `getData' undeclared (first use this function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `FriskyFrisky' undeclared (first use this function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc: In function `int main()':
CatFive.cc:53: error: no match for 'operator<<' in 'std::cout <<
(&Frisky)->CatFive::hi()'
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:74: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits
= std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:86: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
 
A

adbarnet

Hi Hesham,

From the code you posted, I noticed that you had a .h file containing
declarations, and a .cc file containing definitions.
Your using namespace statement was in your .h file, but it wasn't clear how
you were exposing the std namespace in your .cc file

In general, try to not use the using namespace statement in your .h files -
it can cause problems for clients of your library - instead, in your header
explicitly specify the namespace scope, and in your implementation (.cpp)
files use the using namespace notation.

..cc files are an exception to the rule - they are implementation files that
are effectively #included in other implementation files - so can cause the
same problems to client's as .h files - instead, treat them llike header
files in respect to namespaces, and explicitly specify the namespace you are
using.

Hope that helps,

ad



sphenxes said:
Hi ad

Thank you for you reply and help. I still have few questions because I do
not really understand what is going on.
First:
I am using this example from Teach yourself c++ in 21 days. I tried to use
#include <iostream.h> which of course have not work. Then I tried to use
#include <iostram> and using namespace std; which worked with all my
previous programs (it is clear that I am not quit understanding what I am
doing).
You have just found the mistake and it has something to do with namespace.
My question:
where should I use namespace. I have already use it on the second line of
my
program which of course seems not to work,
Would you please elaborate on how and where should I use namespace.

Thank you once more for your help.

Regards
Hesham Marei
sphenxes
It appears you are not 'using' namespace std on you cout and endl's try
std::cout and std::endl - or using namespace statement (not recomended
if
you are including the cc file with your header).

ad

sphenxes said:
#include <iostream>
using namespace std;

class CatFive
{
private:
int itsAge;

public:
void hi(); //source of error
CatFive :: CatFive(int cage);
~CatFive();
void setData(int age);
int getData();
};

//////////////////////////

CatFive :: CatFive(int cage)
{
itsAge = cage;
}

CatFive :: ~CatFive()
{
}

void CatFive :: setData(int age)
{
itsAge = age;
}

int CatFive :: getData()
{
return itsAge;
}

void CatFive :: hi() //source of error
{
cout << " hi and welcome" << endl;
}

////////////////////////////////////
int main()
{
CatFive Frisky(1);

cout << " Frisky is " << Frisky.getData() << "years old" << endl;

Frisky.setData(3);
Frisky.getData(),

cout << Frisky.hi(); //source of error
cout << "Frisky is now " << Frisky.getData()<< "years old" << endl;

return 0;
}


Hi
I try to compile this program but I get a lot of errors because of
void hi(); function,
if I delete this function and all related data, the program is compiled
without any problems.
I am using g++ as a compiler (version 3.3.3) on linux Suse 9.1.
I have compiled similar programs several times before. I can't find the
error on my own.
Any sugession is appreciated.

Regards
Hesham Marei (sphenxes)








Compiler Errors
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `getData' undeclared (first use this function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc:2: error: syntax error before `namespace'
CatFive.cc: In member function `void CatFive::hi()':
CatFive.cc:40: error: `cout' undeclared (first use this function)
CatFive.cc:40: error: (Each undeclared identifier is reported only once
for
each function it appears in.)
CatFive.cc:40: error: `endl' undeclared (first use this function)
CatFive.cc: In function `int main()':
CatFive.cc:54: error: `FriskyFrisky' undeclared (first use this
function)
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc: In function `int main()':
CatFive.cc:53: error: no match for 'operator<<' in 'std::cout <<
(&Frisky)->CatFive::hi()'
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:74: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits
= std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:86: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
 
S

sphenxes

Hi Raymond

Thank you for your reply and I think I must return back to the drawing
board and I must change the void function or as your advice skip it is
better.
It was also helpful from you to point me to the compiler error, where I
should concentrate my effort.
Regards
Hesham Marei


Raymond said:
#include <iostream>
using namespace std;

class CatFive
{
public:
void hi(); //source of error

This function returns void.
void CatFive :: hi() //source of error
{
cout << " hi and welcome" << endl;
}
int main()
{

Frisky.getData(),

Switch the comma with a semi-colon.
cout << Frisky.hi(); //source of error

Right here, you are trying to print the return value of the Frisky.hi(),
which is of type void. Cout does not have an operator that does this -
either change the return value of Frisky.hi(), or avoid passing it to
cout.
chat@linux:~/mydata/myprograms/c++/practice> g++ CatFive.cc -o CatFive
CatFive.cc: In function `int main()':
CatFive.cc:53: error: no match for 'operator<<' in 'std::cout <<
(&Frisky)->CatFive::hi()'

The answer is clearly stated right here. The other lines that follow
describe the available functions that can be used.
/usr/include/g++/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:74: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits
= std::char_traits<char>]
/usr/include/g++/bits/ostream.tcc:86: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top