Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile probs

Status
Not open for further replies.

asm1

Technical User
Oct 9, 2003
77
0
0
GB
please help, when i compile the following code i get errors not sure what they mean though. very new to Linux and C++

Code:
#include <iostream>
int main ()
{
  int localVar = 5;
  int * plocal = &localVar;
  int * pheap = new int;
  *pheap = 7;
  cout << &quot;localVar: &quot; << localVar << &quot;\n&quot;;
  cout << &quot;plocal: &quot; << plocal << &quot;\n&quot;;
  cout << &quot;pheap: &quot; << pheap <<&quot;\n&quot;;
  delete pheap;
  pheap = new int;
  *pheap = 9;
  cout << &quot;pheap: &quot; << pheap <<&quot;\n&quot;;
  delete pheap;
  return 0;
}

[tony@jstlinbox c]$ gcc ccode.cpp -o myoutputccode
ccode.cpp: In function `int main()':
ccode.cpp:11: `cout' undeclared (first use this function)
ccode.cpp:11: (Each undeclared identifier is reported only once for each
function it appears in.)
[tony@jstlinbox c]$

The tougher it gets the more you learn! The more you learn the tougher it gets!
 
cout lives in namespace std.

Therefore, its full name is std::cout.

You can use this name every time you refer to it.

Or you can say &quot;using std::cout&quot; to bring the name into a scope.

Or, as the least preferable alternative, you can say &quot;using namespace std;&quot; at the beginning of your program.
 
thanks for that, now u mention it I actually remember reading it somewhere; I will have a mess

The tougher it gets the more you learn! The more you learn the tougher it gets!
 
i did this but i now get more errors and i can't see the problem. could i be compiling wrong or maybe the .cpp file extension wrong, i just dont know. i have put the alterd programme and errors below thx

Code:
//deleating pointers
//alocating and deleating pointers
#include <iostream>
using std::cout;
using std::endl;

int main ()
{
  int localVar = 5;
  int * plocal = &localVar;
  int * pheap = new int;
  *pheap = 7;
  cout << &quot;localVar: &quot; << localVar << endl;
  //cout << &quot;*plocal: &quot; << *plocal << endl;
  cout << &quot;*pheap: &quot; << *pheap << endl;
  delete pheap;
  pheap = new int;
  *pheap = 9;
  cout << &quot;*pheap: &quot; << *pheap << endl;
  delete pheap;
  cout << endl;
  return 0;
}

and the erros are

Code:
[tony@jstlinbox tony]$ cd ./c
[tony@jstlinbox c]$ gcc ccode.cpp -o outccode
/tmp/ccvV43TY.o(.text+0x23): In function `main':
: undefined reference to `operator new(unsigned)'
/tmp/ccvV43TY.o(.text+0x3a): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccvV43TY.o(.text+0x4d): In function `main':
: undefined reference to `std::cout'
/tmp/ccvV43TY.o(.text+0x52): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccvV43TY.o(.text+0x5b): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
/tmp/ccvV43TY.o(.text+0x64): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char,
they actually go on a lot further

The tougher it gets the more you learn! The more you learn the tougher it gets!
 
ok i have taken the programme back to an hello world and this is what i get

Code:
//deleating pointers
//alocating and deleating pointers

#include <iostream>
using std::cout;
//using std::endl;

int main ()
{
  /*

	int localVar = 5;
  int * plocal = &localVar;
  int * pheap = new int;
  *pheap = 7;
  cout << &quot;localVar: &quot; << localVar << endl;
  //cout << &quot;*plocal: &quot; << *plocal << endl;
  cout << &quot;*pheap: &quot; << *pheap << endl;
  delete pheap;
  pheap = new int;
  *pheap = 9;
  cout << &quot;*pheap: &quot; << *pheap << endl;
  delete pheap;   
       
	*/
	cout << &quot;Hello world&quot;;
  //cout << endl;
  return 0;
}


errors

[tony@jstlinbox c]$
[tony@jstlinbox c]$ gcc hello.cpp -o outhello
/tmp/ccTKztTm.o(.text+0x19): In function `main':
: undefined reference to `std::cout'
/tmp/ccTKztTm.o(.text+0x1e): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccTKztTm.o(.text+0x4a): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccTKztTm.o(.text+0x79): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccTKztTm.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[tony@jstlinbox c]$

The tougher it gets the more you learn! The more you learn the tougher it gets!
 
You're trying to use gcc (the C compiler frontend) to compile a C++ program.

You want to be using g++ (the C++ compiler frontend).
 
Also, you may want to include the whole namespace if your just getting started with the language, as there might a lot of things in the name space you'll need... You can always pare the namespace down to what you used after it's ready for realse.
 
Thanks guys, feel a bit silly now I have been messing for two days.
[bugeyed]

The tougher it gets the more you learn! The more you learn the tougher it gets!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top