PDA

View Full Version : C++




borok
Feb 5, 2009, 03:59 PM
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:

#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.



Tygernoot
Feb 5, 2009, 04:32 PM
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.

borok
Feb 5, 2009, 04:47 PM
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

eddietr
Feb 5, 2009, 05:06 PM
When i do put main() i get an error:
Command /Developer/usr/bin/g++ failed with exit code 1



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.


I found a thread online, and read bout changing main to main1.



That doesn't sound like very good advice.

borok
Feb 5, 2009, 05:18 PM
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

eddietr
Feb 5, 2009, 05:24 PM
So here is the key:


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.

borok
Feb 5, 2009, 05:48 PM
so what should i do and how...
thanks

eddietr
Feb 5, 2009, 05:53 PM
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.

superwoman
Feb 5, 2009, 05:54 PM
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?

borok
Feb 5, 2009, 07:09 PM
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.

eddietr
Feb 5, 2009, 07:18 PM
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.

And you checked exo1.cpp and you're sure there is no main() there?

gnasher729
Feb 5, 2009, 07:35 PM
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.

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

borok
Feb 5, 2009, 07:37 PM
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.

gnasher729
Feb 5, 2009, 07:54 PM
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.

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.