when i compile, it gives the following error :

Undefined first referenced
symbol in file
main /auto/usc/gnu/gcc/4.2.1/bin/../lib/gcc/sparc-sun-solaris2.10/4.2.1/crt1.o
ld: fatal: Symbol referencing errors. No output written to a
collect2: ld returned 1 exit status

Can anyone please tell as to what's going wrong here.

I have some code where i want to have one class keeping the data members and the other 
having a member function and another having the main in it. I have made a prototype
of what i want to do for simplicity. 

#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
	
using namespace std;

class M
{
	public:
		int value1;
		int value2;
};

class P
{
	public:
		int sum(M n){	
			int t = 2;
			int s = n.value2 + t;
			return s;
		}
};

class N
{
	int main(){

		M m;

		P p;

		m.value1 = 1;
		m.value2 = 2;
		
		int res = p.sum(m); 	
		cout << "sum is" << res;	
	}
};

Recommended Answers

All 8 Replies

I compiled it with minGW in code blocks and it worked fine (after I added return; and took main() out of a class) so maybe its something to do with the compiler/ ide your using

I am using aludra on Sun Solaris. Thanks any ways for that. Atleast I know that it works, but I need to get it working on this platform only.

have you tried this code?

#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class M
{
	public:
		int value1;
		int value2;
};

class P
{
	public:
		int sum(M n){
			int t = 2;
			int s = n.value2 + t;
			return s;
		}
};

int main(){

    M m;

    P p;

    m.value1 = 1;
    m.value2 = 2;

    int res = p.sum(m);
    cout << "sum is: " << res;
    cin.get();
    return 0;
}

Oh yes I did....i tried to give the main a return value too but the error persists....

Why is main() inside class N???

By the way your code compiles and works on Solaris, but only if main() is removed from the class. Just copy 'n' past MattyRobot's code into your IDE and see for yourself. ;)

Oh yes it does....thanks MattyRobot and necrolin
but just out of curiosity - why does the main definition in the class is an error

The program is supposed to start at main(). Classes on the other hand don't do anything until you create an object and start executing member functions. So, main() would never get executed. Also, remember that classes default to "private:" meaning that only class N would have access to main().

yay i solved something:):):):icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.