C++

Discussion in 'Mac Programming' started by borok, Feb 5, 2009.

  1. macrumors newbie

    Joined:
    Feb 5, 2009
    #1
    Hi the program is mostly done, i am trying to compile it and thats when i have problems; I only get a blank screen after i run it.

    thats the program:
    Code:
    #include "exo1.h"
    
    #include <iostream>
    using namespace std;
    #ifndef NONVIRTUELLE_H
    #define NONVIRTUELLE_H
    
    class NonVirtuelle {
    	int x;
    public:
        void a() const {}
        int b() const { return 1; }
    };
    #endif
    
    #ifndef UNEVIRTUELLE_H
    #define UNEVIRTUELLE_H
    class UneVirtuelle {
        int x;
    public:
        virtual void a() const {}
        int b() const { return 1; }
    };
    #endif
    
    #ifndef DEUXVIRTUELLES_H
    #define DEUXVIRTUELLES_H 
    class DeuxVirtuelles {
        int x;
    public:
        virtual void a() const {}
        virtual int b() const { return 1; }
    };
    #endif
    
    int main1() {
        cout << "La taille d'un entier: " << sizeof(int) << endl;
        cout << "La taille de NonVirtuelle: " << sizeof(NonVirtuelle) << endl;
        cout << "La taille d'un pointeur void: " << sizeof(void*) << endl;
        cout << "La taille de UneVirtuelle: " << sizeof(UneVirtuelle) << endl;
        cout << "La taille de DeuxVirtuelles: " << sizeof(DeuxVirtuelles) << endl;
        return 0;
    }
    
    I would like to know whats the problem and how to correct it.

    if anyone can post me the results of that program, i would appreciate it.
    Thanks for help.
     
  2. macrumors member

    Joined:
    Jul 3, 2003
    Location:
    London
    #2
    Is main1() your application's main function?

    Then it should be named main() instead of main1(). Otherwise you should call main1() from your main() function.

    Hope this helps.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
    #3
    When i do put main() i get an error:
    Command /Developer/usr/bin/g++ failed with exit code 1
    I found a thread online, and read bout changing main to main1.

    I am new in Mac, first time using Xcode.

    Thank You
     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    Can you print more of the error? That just says that gcc failed. But before that there should be much more info about why it failed.

    That doesn't sound like very good advice.
     
  5. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
    #5
    that's all the error msg:

    Building target “tp1” of project “tp1” with configuration “Debug”


    Checking Dependencies

    Ld /Users/alisobh/Desktop/tp1/build/Debug/tp1.app/Contents/MacOS/tp1 normal i386
    cd /Users/alisobh/Desktop/tp1
    setenv MACOSX_DEPLOYMENT_TARGET 10.5
    /Developer/usr/bin/g++-4.0 -arch i386 -isysroot /Developer/SDKs/MacOSX10.5.sdk -L/Users/alisobh/Desktop/tp1/build/Debug -F/Users/alisobh/Desktop/tp1/build/Debug -filelist /Users/alisobh/Desktop/tp1/build/tp1.build/Debug/tp1.build/Objects-normal/i386/tp1.LinkFileList -mmacosx-version-min=10.5 -framework Carbon -o /Users/alisobh/Desktop/tp1/build/Debug/tp1.app/Contents/MacOS/tp1
    ld: duplicate symbol _main in /Users/alisobh/Desktop/tp1/build/tp1.build/Debug/tp1.build/Objects-normal/i386/exo1.o and /Users/alisobh/Desktop/tp1/build/tp1.build/Debug/tp1.build/Objects-normal/i386/main.o
    collect2: ld returned 1 exit status


    thanks
     
  6. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    So here is the key:

    Code:
    ld: duplicate symbol _main in /Users/alisobh/Desktop/tp1/build/tp1.build/Debug/tp1.build/Objects-normal/i386/exo1.o and /Users/alisobh/Desktop/tp1/build/tp1.build/Debug/tp1.build/Objects-normal/i386/main.o
    So it looks like you have another object file called exo1.o which also has a main() function.
     
  7. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
  8. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #8
    Well, so, after you compile this program you are writing, everything will be linked together, and then when you run it the main() function will be called.

    So depending on what you are trying to do, you need to decide which main() function should be the main function for the app. Then if you need to call the other function, you can always do so from within main().

    But you can't have two main() functions.

    So which main() do you want is the question? Decide that and then rename the other one.
     
  9. macrumors regular

    Joined:
    Apr 25, 2005
    Location:
    Monterey,CA
    #9
    What is "exo1"?

    I noticed you include "exo1.h" in your code.

    Can you check exo1.cpp to see if there's a main function in it?
     
  10. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
    #10
    that's the problem, i only have 1 main(), which is the main fonction that i know about!
    exo.h is the header...when i created a new project it opened exo1.h and exo1.cpp.
     
  11. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #11
    And you checked exo1.cpp and you're sure there is no main() there?
     
  12. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    You have only one main () function and it looks like you have no idea where it is. You also have a function named main1 (), but it doesn't matter one bit what it does, because it never gets called.

    Well, if you read those error messages carefully, the linker told you exactly where those two functions named main () were. Here's the error message again:

    ld: duplicate symbol _main in /Users/alisobh/Desktop/tp1/build/tp1.build/Debug/tp1.build/Objects-normal/i386/exo1.o and /Users/alisobh/Desktop/tp1/build/tp1.build/Debug/tp1.build/Objects-normal/i386/main.o
     
  13. thread starter macrumors newbie

    Joined:
    Feb 5, 2009
    #13
    the whole program is in the first thread i posted. the only difference now is that i corrected main1() to main().
    when its main1() the program succeeds but it opens a blank screen w/o anything.
     
  14. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #14
    No, it isn't. You posted what you THINK is the whole program. You didn't post the whole program. Read the bloody error message again. I even marked in bold exactly where the second main () function is; you just have to read it.

    In XCode, press Command-Zero to show the project overview, then reveal "Implementation Files". Then think very very hard about what you see there.
     

Share This Page