By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Compiling with g++ - Error - Help

P: n/a
Hi,

I am trying to figure out why this code doesn't work when I compile it
under linux with g++

Following is the code, and the error message I get is:

main.cpp: In function ‚int main()‚:
main.cpp:5: error: conversion from ‚Person*‚ to non-scalar type
‚Person‚ requested

I used the following command to compile: g++ Person.h Person.cpp
main.cpp

------------------------------------
//filename: Person.h
class Person
{
private:
int age;
public:
void setAge(int age);
};

--------------------------------------
//filename: Person.cpp
#include "Person.h"

void Person::setAge(int age)
{
this->age = age;
}

--------------------------------------
//filename: main.cpp
#include "Person.h"

int main()
{
Person mark = new Person();
mark.setAge(5);

return 0;
}

----------------------------------------

I am not sure what the error means, but everything looks okay to me.
Looking forward to someone helping me out on this.

Thank you!

Ali

Feb 24 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
al*************@gmail.com wrote:
>
I am trying to figure out why this code doesn't work when I compile it
under linux with g++
Because C++ isn't Java.
Person mark = new Person();
Person *mark = new Person();

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 24 '07 #2

P: n/a
Hi


<al*************@gmail.comwrote in message
news:11**********************@z35g2000cwz.googlegr oups.com...
Hi,

I am trying to figure out why this code doesn't work when I compile it
under linux with g++

Following is the code, and the error message I get is:

main.cpp: In function ‚int main()‚:
main.cpp:5: error: conversion from ‚Person*‚ to non-scalar type
‚Person‚ requested

I used the following command to compile: g++ Person.h Person.cpp
main.cpp

------------------------------------
//filename: Person.h
#include <memory>// <++++++++++++++++
class Person
{
private:
int age;
public:
void setAge(int age);
};

--------------------------------------
//filename: Person.cpp
#include "Person.h"

void Person::setAge(int age)
{
this->age = age;
}

--------------------------------------
//filename: main.cpp
#include "Person.h"

int main()
{
auto_ptr<Personmark = new Person(); // <++++++++++++++++
mark->setAge(5);// <++++++++++++++++
return 0;
}

----------------------------------------

I am not sure what the error means, but everything looks okay to me.
Looking forward to someone helping me out on this.

Thank you!

Ali
See the changes.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu


Feb 24 '07 #3

P: n/a
Sorry,

Don't forget to throw in a

using namespace std;

In the source file :-)


Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Ron AF Greve" <news moonlit xs4all nlwrote in message
news:45*********************@news.xs4all.nl...
Hi


<al*************@gmail.comwrote in message
news:11**********************@z35g2000cwz.googlegr oups.com...
Hi,

I am trying to figure out why this code doesn't work when I compile it
under linux with g++

Following is the code, and the error message I get is:

main.cpp: In function ‚int main()‚:
main.cpp:5: error: conversion from ‚Person*‚ to non-scalar type
‚Person‚ requested

I used the following command to compile: g++ Person.h Person.cpp
main.cpp

------------------------------------
//filename: Person.h
#include <memory>// <++++++++++++++++
class Person
{
private:
int age;
public:
void setAge(int age);
};

--------------------------------------
//filename: Person.cpp
#include "Person.h"

void Person::setAge(int age)
{
this->age = age;
}

--------------------------------------
//filename: main.cpp
#include "Person.h"

int main()
{
auto_ptr<Personmark = new Person(); // <++++++++++++++++
mark->setAge(5);// <++++++++++++++++
return 0;
}

----------------------------------------

I am not sure what the error means, but everything looks okay to me.
Looking forward to someone helping me out on this.

Thank you!

Ali
See the changes.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu


Feb 24 '07 #4

P: n/a
On 25 Feb., 00:33, aliasger.jaf...@gmail.com wrote:
Hi,

I am trying to figure out why this code doesn't work when I compile it
under linux with g++
It has nothing to do with linux og g++
>
Following is the code, and the error message I get is:

main.cpp: In function "int main()":
main.cpp:5: error: conversion from "Person*" to non-scalar type
"Person" requested
[snip]
{
Person mark = new Person();
mark.setAge(5);
[snip]
I am not sure what the error means, but everything looks okay to me.
I find the errormessage quite good. You are using a Person* where you
should have used a Person. A pointer does not support operator dot -
and that should be basic knowledge.
Why do you use operator new? Corrected program is

Person mark;
mark.setAge(5);

/Peter

Feb 24 '07 #5

P: n/a
ali
On Feb 24, 5:44 pm, "Ron AF Greve" <news moonlit xs4all nlwrote:
Hi

<aliasger.jaf...@gmail.comwrote in message

news:11**********************@z35g2000cwz.googlegr oups.com...
Hi,

I am trying to figure out why this code doesn't work when I compile it
under linux with g++

Following is the code, and the error message I get is:

main.cpp: In function ‚int main()‚:
main.cpp:5: error: conversion from ‚Person*‚ to non-scalar type
‚Person‚ requested

I used the following command to compile: g++ Person.h Person.cpp
main.cpp

------------------------------------
//filename: Person.h
#include <memory>// <++++++++++++++++

class Person
{
private:
int age;
public:
void setAge(int age);

};

--------------------------------------
//filename: Person.cpp
#include "Person.h"

void Person::setAge(int age)
{
this->age = age;

}

--------------------------------------
//filename: main.cpp
#include "Person.h"

int main()
{
auto_ptr<Personmark = new Person(); // <++++++++++++++++
mark->setAge(5);// <++++++++++++++++

return 0;

}

----------------------------------------
Oh, I get it. Thanks a bunch!!...
>
I am not sure what the error means, but everything looks okay to me.
Looking forward to someone helping me out on this.

Thank you!

Ali

See the changes.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

Feb 25 '07 #6

P: n/a
al*************@gmail.com wrote:
....
>
int main()
{
Person mark = new Person();
// try this :-
Person mark;
mark.setAge(5);

return 0;
}
There is no need to call new - you must subsequently call delete if you
do use new.
Feb 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.