"Conversion from 'Person*' to non-scalar type 'Person' requested in OMNet++

I got this error when I tried to instantiate a class in my constructor. I'm not sure what the non-scalar type error means. I looked it up in various forums but all of those situations involved a person trying to mix types (ie passing an int into the class when the constructor calls for a bool or something). But here my class has no constructor parameters. I'm not trying to mix types as far as I know so don't know why I'm getting this error. I'm new to C++ so I'm not familiar with errors and how to instantiate classes.

This is the function with the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <omnetpp.h>
#include <string.h>
#include "nmessage_m.h"
#include "Person.cc"
#include <math.h>

class PersonC : public cSimpleModule
{
	public:
		PersonC();
		int p,q,B;
	private:
		virtual void initialize();
		virtual void handleMessage(cMessage *msg);
		int seq,a,r;
};

Define_Module(PersonC);

PersonC::PersonC(){
	Person c = new Person();//*This is where the error occurs
	q = c.Q;
	r = 2;
	p = 4;
	a = 6;
	B = 4;
	seq=0;
}

** note there is more code where I define handleMessage and initialize but it's not relevant

This is the Person class I'm creating an instance of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <string.h>
#include <math.h>
#include <omnetpp.h>
#include <cSimpleModule.h>


class Person : public cSimpleModule{
	private:
		void initialize();
		void handleMessage(cMessage *msg);
		int randprime();

	public:
		static int P,Q,T,b,A,R;
		int getQ();
		int getP();
		int getT();
		int getB();
		Person();

};


Define_Module(Person);

Person::Person(){
	Q = randprime();
	T = 5;
	R = rand() % Q + 1;
	P = Q*R+1;
	A = 2;
	b =(A^((P-1)/Q))%P;
}

int Person::getQ(){
	return Q;
}
int Person::getP(){
	return P;
}
int Person::getT(){
	return T;
}
int Person::getB(){
	return b;
}

void Person::initialize(){
	send(new cMessage("message"), "out");
}

void Person::handleMessage(cMessage *msg){
	send(msg,"out");
}
int Person::randprime(){
	bool prime=true;
	int num= 0;
	do{
	num = rand() % 100 + 1;
	prime = true;
		for( int i=2; i<num; i++){
			prime = ((num%i)==0);
		}
	} while(!prime);
	return num;
}


I'm writing this code on a windows machine in a program called OMNeT++, a discrete event simulator.
Also, what I'm trying to do is allow multiple different classes to have access to the static int variables in Person. As you can see though some of those variables are not constant and would be different every time I created a new instance, but I don't want that. I need the program to create a random number and then let any other class get access to that particular random number.
To answer your first question: Using new to instantiate a class dynamically allocates memory for an object of that class on the heap. The value that is returned from this operation is a pointer to the memory location for that instance. Hence, the compiler is complaining that you're trying to assign a pointer value to a non-pointer variable c (of type Person). To fix that, simply change line 21 to:

Person * c = new Person();
(Note the * symbol)

Also note that you should free this memory using the delete keyword when you're finished using that object.

To attempt to answer your second question (and I stress that there's probably a better way to do it!): When I read this, I thought that the Singleton pattern might be useful here. You could create a class to store a random number, and this class can only be instantiated once (i.e. a singleton). You could pass a reference to this object to each of your Person instances that you create. Then, each of your Person instances will have access to the same random number object.

Explanation of what the Singleton pattern is: http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B

@More experienced posters: Please feel free to jump on this suggestion...I'd like to know if it's overkill.

Hope it helps.
Thank you, the * did fix that problem. I looked at the Singleton method and it looked pretty close to what I'm wanting to do. But when I tried to implement it, it gave me similar errors I had a while ago that led me to try the method above.

Here is my singleton class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <omnetpp.h>
#include <string.h>
#include <math.h>

class Singleton
{
	private:
		static int Q;
		static bool isMade;
		//static Singleton s;
		Singleton(const Singleton &);             // intentionally undefined
		Singleton & operator=(const Singleton &); // intentionally undefined

	public:
		Singleton() {isMade = false;}
		~Singleton() {}
		//static Singleton &getInstance();
		static int getInstance();
		virtual int randprime();
};


// Source file (.cpp)
int Singleton::getInstance(){
	if(isMade){
		return Q;
	}
	else{
		Singleton * s = new Singleton();
		isMade = true;
		Q = s->randprime();
		return Q;
	}
}

int Singleton::randprime(){
	bool prime=true;
	int num= 0;
	do{
	num = rand() % 100 + 1;
	prime = true;
		for( int i=2; i<num; i++){
			prime = ((num%i)==0);
		}
	} while(!prime);
	return num;
}


I'm sure there are problems with it. I'm not exactly sure how the singleton pattern works and I feel like my code has made it's way back to how I was doing it before.

But I'm getting a few errors on line 21:
undefined reference to 'Singleton::isMade'
undefined reference to 'Singleton::Q'
multiple definition of Singleton::getInstance()

This multiple definition error comes up any time I try to include these class files in another file, but they go away when the include is gone. (e.g. I include the singleton.cc file in my PersonC class file, but I don't do anything with Singleton in the class and the multiple definitions error pops up). I don't know where this multiple definition is though.


Topic archived. No new replies allowed.