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;	
	}
};

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().

Edited 7 Years Ago by necrolin: n/a

This article has been dead for over six months. Start a new discussion instead.