Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mmiller039

macrumors newbie
Original poster
Jan 4, 2008
3
0
I am trying to impement the simplest of programs,


#include <iostream>
using namespace std;

int x = 1;

cout >> "hello world";


but I get,


C++_Prefix-dwhjsqjyhnjzkbbxypqkxsqhiyir/Learn C++_Prefix.pch" -c "/Users/mark_g_miller/Learn C++/Tutorial1.cpp" -o "/Users/mark_g_miller/Learn C++/build/Learn C++.build/Release/Learn C++.build/Objects-normal/i386/Tutorial1.o"
/Users/mark_g_miller/Learn C++/Tutorial1.h:15: error: expected constructor, destructor, or type conversion before '<<' token
/Users/mark_g_miller/Learn C++/Tutorial1.h:15: error: expected constructor, destructor, or type conversion before '<<' token


and know that it relates to the statement as when I remove it my debug runs through the code. I apologise for the simplicity but have no idea how to solve.

Any answers - mmiler039@gmail.com
 

titaniumdecoy

macrumors member
Oct 13, 2005
86
0
Code:
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    cout << "hello world";
    return 0;
}
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
Post your entire Tutorial1.cpp file (with copy and paste in a code block). I suspect there is a minor typo somewhere, but the compiler doesn't realize it until it gets to the cout line. Also, if Tutorial1.cpp includes Tutorial1.h, post that, too.

Check for a missing or extra curly brace: { or }
 

mmiller039

macrumors newbie
Original poster
Jan 4, 2008
3
0
New error

trial.h

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
cout << "hello world";
return 0;
}​

trial.cpp

#include "trial.h"​

Error

cd /Users/mark_g_miller/Newtrial
/Developer/usr/bin/g++-4.0 -o /Users/mark_g_miller/Newtrial/build/Newtrial.build/Release/Newtrial.build/Objects-normal/ppc/Newtrial -L/Users/mark_g_miller/Newtrial/build/Release -F/Users/mark_g_miller/Newtrial/build/Release -filelist /Users/mark_g_miller/Newtrial/build/Newtrial.build/Release/Newtrial.build/Objects-normal/ppc/Newtrial.LinkFileList -framework Carbon -arch ppc -mmacosx-version-min=10.5 -Wl,-dead_strip -no_dead_strip_inits_and_terms -isysroot /Developer/SDKs/MacOSX10.5.sdk
ld: duplicate symbol _main in /Users/mark_g_miller/Newtrial/build/Newtrial.build/Release/Newtrial.build/Objects-normal/ppc/trial.o and /Users/mark_g_miller/Newtrial/build/Newtrial.build/Release/Newtrial.build/Objects-normal/ppc/main.o

collect2: ld returned 1 exit status
ld: duplicate symbol _main in /Users/mark_g_miller/Newtrial/build/Newtrial.build/Release/Newtrial.build/Objects-normal/ppc/trial.o and /Users/mark_g_miller/Newtrial/build/Newtrial.build/Release/Newtrial.build/Objects-normal/ppc/main.o
collect2: ld returned 1 exit status​

Thanks guys, am a bit of a newby to all this
 

titaniumdecoy

macrumors member
Oct 13, 2005
86
0
You don't need a header file for this program. Save the code I posted above as trial.cpp and compile and run it as follows (first cd to the directory containing trial.cpp):

$ g++ -o trial trial.cpp
$ ./trial

You also clearly do not understand the concept of header files (no offense intended) so I suggest reading a tutorial on such.
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
Yeah, TD is right, don't try to use header files until you get to that part of the tutorial.

By the way, the error you are getting is because main() is defined twice (or more times) in your project. You might be including your trial.h twice--either in the same .cpp file, or in different .cpp files. Or you might have another main() function in another .cpp file that is part of your XCode project.

It seems that some of your problems have to do with not understanding XCode. Does the book you are using introduce a programming environment (like gcc at the command-line, etc)? If so, consider using that environment while you are getting started so you don't have to worry about learning XCode at the same time as C--one thing at a time is enough!
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
From what you've posted, I have a hunch you're working in XCode and, assuming you're using the correct template for this project (Command Line Utility/C++ Tool), might not have noticed it creates a main.cpp file for you that already contains almost verbatim the "hello world" main() implementation you've put into trial.*. That would explain why your project is trying to link in a "main.o" that already contains a _main symbol.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.