LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2006, 02:22 PM   #1
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Rep: Reputation: 15
How do I compile?


Im trying to compile this simple ******

#include <iostream>
int main(void)

{

cout << "WORK FFS!";
cout << "\n";
return 0;

}


what have I done wrong? beacouse it says when i try to compile cc test.cpp

ali@pullansreturn:~/programing$ cc test.cpp
test.cpp: In function 'int main()':
test.cpp:6: error: 'cout' was not declared in this scope



and where does the runnable file get when if I ever succeding compiling? seriously i dont know whats wrong. HELP!
 
Old 02-13-2006, 02:35 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
if you compile your program with the following command
Code:
g++ -o test.o test.cpp
you will get an output file (-o switch) with the name test.o which you can run with the command
Code:
./test.o
This file will be in the same directory as the source code. Using a path you can get a different location.

Your code is using cout which resides in the std namespace and so you will need to modify each cout, what follows is an example of what you will need to do:
Code:
std::cout << "Hello";
 
Old 02-13-2006, 02:51 PM   #3
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Original Poster
Rep: Reputation: 15
OK, I have seen how people use "using namespace std;" but it doesnt work, i guess it does the same thing as std::cout << "Hello"; But it still doesnt work


include <iostream>
int main(void)
using namespace std;

{

cout << "WORK FFS!";
cout << "\n";
return 0;

}


ali@pullansreturn:~/programing$ g++ -o test.o test.cpp
test.cpp:3: error: expected initializer before 'using'
test.cpp:5: error: expected unqualified-id before '{' token
ali@pullansreturn:~/programing$
 
Old 02-13-2006, 02:53 PM   #4
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Original Poster
Rep: Reputation: 15
#include <iostream>
using namespace std;
int main(void)
{

cout << "WORK FFS!";
cout << "\n";
return 0;

}
~
~


it works now! thanks!
 
Old 02-13-2006, 02:55 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
(nevermind)

Last edited by spooon; 02-13-2006 at 02:57 PM.
 
Old 02-13-2006, 03:04 PM   #6
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Original Poster
Rep: Reputation: 15
Yeah i know what you were going to mention, that int main(void) is the head of the { } body and that i typed using namspace std; between them
 
Old 02-13-2006, 03:13 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Indeed using std:: each time you use a function from the standard libraries is the same as putting in a using namespace std; at the start of the file however I have a preference towards std:: because it makes it quite clear that it is a standard function that is being used.

Also you can replace a "\n" with a std::endl which can all be on one line as follows:
Code:
std::cout << "Hello" << std::endl;
graeme.
 
Old 02-13-2006, 03:19 PM   #8
inverted.gravity
Member
 
Registered: Feb 2006
Location: Sweden
Distribution: Archlinux
Posts: 112

Original Poster
Rep: Reputation: 15
well to make this clear, im a total noob in all aspects of programing, i just started for like an half hour ago
so i dont really know what to do, i dont even know what using namespaces std; is not even int main(void) lol
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot compile jonn Linux - Software 2 03-28-2005 02:37 PM
can't compile anything Cyber Maid Linux - Software 12 02-12-2005 06:58 PM
ntop compile/post-compile problem? tjb Linux - Software 3 12-28-2004 04:22 PM
Cannot compile c davidtsl Programming 8 12-13-2004 01:36 AM
why won't this compile blackzone Programming 1 09-17-2004 04:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration