| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"sphenxes" |
| Date: |
05 Dec 2004 07:10:20 AM |
| Object: |
The compiler do not recognize my function (newbie). |
#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>::operator<<(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>::operator<<(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>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
.
|
|
| User: "Rade" |
|
| Title: Re: The compiler do not recognize my function (newbie). |
05 Dec 2004 10:24:48 AM |
|
|
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
.
|
|
|
|
| User: "adbarnet" |
|
| Title: Re: The compiler do not recognize my function (newbie). |
05 Dec 2004 08:06:28 AM |
|
|
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" <sphenxes@none.net> wrote in message
news:1102252197.ZrqGIQcJbmvbKGFCyg48kw@teranews...
#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>::operator<<(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>::operator<<(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>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
.
|
|
|
| User: "sphenxes" |
|
| Title: Re: The compiler do not recognize my function (newbie). |
05 Dec 2004 10:41:28 AM |
|
|
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" <sphenxes@none.net> wrote in message
news:1102252197.ZrqGIQcJbmvbKGFCyg48kw@teranews...
#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>::operator<<(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>::operator<<(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>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
.
|
|
|
| User: "adbarnet ad@dontmailmethx" |
|
| Title: Re: The compiler do not recognize my function (newbie). |
05 Dec 2004 06:09:55 PM |
|
|
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" <sphenxes@none.net> wrote in message
news:1102264863.+P4oRMvYYlCwUlz+X9ympw@teranews...
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" <sphenxes@none.net> wrote in message
news:1102252197.ZrqGIQcJbmvbKGFCyg48kw@teranews...
#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>::operator<<(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>::operator<<(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>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.
|
|
|
|
|
|
| User: "Raymond Martineau" |
|
| Title: Re: The compiler do not recognize my function (newbie). |
05 Dec 2004 09:21:21 AM |
|
|
On Sun, 05 Dec 2004 14:10:20 +0100, sphenxes <sphenxes@none.net> wrote:
#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>::operator<<(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>::operator<<(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>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
.
|
|
|
| User: "sphenxes" |
|
| Title: Re: The compiler do not recognize my function (newbie). |
06 Dec 2004 10:17:13 AM |
|
|
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 Martineau wrote:
On Sun, 05 Dec 2004 14:10:20 +0100, sphenxes <sphenxes@none.net> wrote:
#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>::operator<<(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>::operator<<(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>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT =
char,
.
|
|
|
|
|

|
Related Articles |
|
|