Error message when trying to use cout command in C++

Discussion in 'Mac Programming' started by mmiller039, Jan 4, 2008.

  1. mmiller039 macrumors newbie

    Joined:
    Jan 4, 2008
    #1
    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
     
  2. mmiller039 thread starter macrumors newbie

    Joined:
    Jan 4, 2008
    #2
    Addition

    code actually has the cout with << not the >> statement
     
  3. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #3
    Code:
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        cout << "hello world";
        return 0;
    }
     
  4. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #4
    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 }
     
  5. mmiller039 thread starter macrumors newbie

    Joined:
    Jan 4, 2008
    #5
    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
     
  6. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #6
    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.
     
  7. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #7
    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!
     
  8. Gelfin macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #8
    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.
     

Share This Page