Compiling with g++ - Error - Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aliasger.jaffer@gmail.com

    Compiling with g++ - Error - Help

    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

  • Pete Becker

    #2
    Re: Compiling with g++ - Error - Help

    aliasger.jaffer @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)

    Comment

    • Ron AF Greve

      #3
      Re: Compiling with g++ - Error - Help

      Hi




      <aliasger.jaffe r@gmail.comwrot e in message
      news:1172360020 .404241.118690@ z35g2000cwz.goo glegroups.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<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






      Comment

      • Ron AF Greve

        #4
        Re: Compiling with g++ - Error - Help

        Sorry,

        Don't forget to throw in a

        using namespace std;

        In the source file :-)




        Regards, Ron AF Greve



        "Ron AF Greve" <news moonlit xs4all nlwrote in message
        news:45e0cde2$0 $324$e4fe514c@n ews.xs4all.nl.. .
        Hi
        >
        >
        >
        >
        <aliasger.jaffe r@gmail.comwrot e in message
        news:1172360020 .404241.118690@ z35g2000cwz.goo glegroups.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<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
        >

        >
        >
        >
        >

        Comment

        • peter koch

          #5
          Re: Compiling with g++ - Error - Help

          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

          Comment

          • ali

            #6
            Re: Compiling with g++ - Error - Help

            On Feb 24, 5:44 pm, "Ron AF Greve" <news moonlit xs4all nlwrote:
            Hi
            >
            <aliasger.jaf.. .@gmail.comwrot e in message
            >
            news:1172360020 .404241.118690@ z35g2000cwz.goo glegroups.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<Person mark = 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

            Comment

            • Gianni Mariani

              #7
              Re: Compiling with g++ - Error - Help

              aliasger.jaffer @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.

              Comment

              Working...