Compiling with g++ - Error - Help

A

aliasger.jaffer

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
 
R

Ron AF Greve

Hi




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


See the changes.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
P

peter koch

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
 
A

ali

Hi


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<Person> mark = new Person(); // <++++++++++++++++
mark->setAge(5);// <++++++++++++++++

return 0;

}

----------------------------------------
Oh, I get it. Thanks a bunch!!...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top