簡體   English   中英

C ++類指針給出錯誤

[英]C++ class pointer giving error

在我的程序中,我創建了一個Object類,它並不過分復雜(有很多構造函數類型,但這很重要)。 我已經看過用於創建指向類的指針的語法,但似乎找不到問題,但它不斷產生此錯誤代碼

錯誤:“ Object(*)()”類型的值不能用於初始化“ Object *”類型的實體

系統信息:Windows Embedded 8.1 x64
IDE:Microsoft Visual Studio 2013更新4

Object.h:

#ifndef OBJECT_H
#define OBJECT_H

#include "Point.h"

class Object {
    private:
        float mf_Mass;
        float mf_Volume;
        int mn_ID;
        bool mb_Parent;
        bool mb_Child;
        Point position;
        Object* mp_Child;
        Object* mp_Parent;

    public:
        //ID generation
        static int c_IDGenerator;
        void generateID(){ mn_ID = c_IDGenerator++; return; }

        //Constructor Prototypes
        Object();

        Object(Point point);
        Object(Object* child);
        Object(float mass);

        Object(Point point, Object* child);
        Object(Point point, float mass);
        Object(Object* child, Object* parent);
        Object(Object* child, float mass);
        Object(float mass, float volume);

        Object(Point point, Object* child, Object* parent);
        Object(Point point, Object* child, float mass);
        Object(Object* child, Object* parent, float mass);
        Object(Object* child, float mass, float volume);

        Object(Point point, Object* child, Object* parent, float mass);
        Object(Point point, Object* child, float mass, float volume);
        Object(Object* child, Object* parent, float mass, float volume);

        Object(Point point, Object* child, Object* parent, float mass, float volume);

        //Get and Set methods
        float getMass(){ return mf_Mass; }
        float getVolume(){ return mf_Volume; }
        int getID(){ return mn_ID; }
        Point getPosition(){ return position; }
        Object& getChild(){ return &(mp_Child); }
        Object& getParent(){ return &(mp_Parent); }
        bool isParent(){ return mb_Parent; }
        bool isChild(){ return mb_Child; }

        void setMass(float mass){ mf_Mass = mass; return; }
        void setVolume(float volume){ mf_Volume = volume; return; }
        void setChild(Object* child){ mp_Child = child; return; }
        void setParent(Object* parent){ mp_Parent = parent; return; }

        //Object Interface
        void move(int x, int y);
        void move(Point point);

        void destroyChild();
        void resetChild();

        void resetParent();

        float calculateDensity();
}

#endif


Object.cpp:

#include "Object.h" 

//Object Constructors with 0 known field(s)
    Object::Object()
    :   position(0, 0)
    {
        mf_Mass = 1;
        mf_Volume = 1;
        mp_Child = 0;
        mp_Parent = 0;
        mb_Child = false;
        mb_Parent = false;
        generateID();
    }

//Object Constructors with 1 known field(s)
    Object::Object(Point point)
    :   position(point)
    {
        mf_Mass = 1;
        mf_Volume = 1;
        mp_Child = 0;
        mp_Parent = 0;
        mb_Child = false;

        generateID();
    }

    Object::Object(Object* child)
    :   position(0, 0)
    {
        mf_Mass = 1;
        mf_Volume = 1;
        mp_Child = child;
        mp_Parent = 0;
        generateID();
    }

    Object::Object(float mass)
    :   position(0, 0)
    {
        mf_Mass = mass;
        mf_Volume = 0;
        mp_Child = 0;
        mp_Parent = 0;
        generateID();
    }

//Object Constructors with 2 known field(s) 
    Object::Object(Point point, Object* child)
    :   position(point)
    {
        mf_Mass = 1;
        mf_Volume = 1;
        mp_Child = child;
        mp_Parent = 0;
        generateID();
    }

    Object::Object(Point point, float mass)
    :   position(point)
    {
        mf_Mass = mass;
        mf_Volume = 1;
        mp_Child = 0;
        mp_Parent = 0;
        generateID();
    }

    Object::Object(Object* child, Object* parent)
    : position(0, 0)
    {
        mf_Mass = 1;
        mf_Volume = 1;
        mp_Child = child;
        mp_Parent = parent;
        generateID();
    }

    Object::Object(Object* child, float mass)
    :   position(0, 0)
    {
        mf_Mass = mass;
        mf_Volume = 1;
        mp_Child = child;
        mp_Parent = 0;
        generateID();
    }

    Object::Object(float mass, float volume)
    :   position(0, 0)
    {
        mf_Mass = mass;
        mf_Volume = volume;
        mp_Child = 0;
        mp_Parent = 0;
        generateID();
    }

//Object Constructors with 3 known field(s)
    Object::Object(Point point, Object* child, Object* parent)
    :   position(point)
    {
       mf_Mass = 1;
       mf_Volume = 1;
       mp_Child = child;
       mp_Parent = parent;
       generateID();
    }

    Object::Object(Point point, Object* child, float mass)
    :   position(point)
    {
        mf_Mass = mass;
        mf_Volume = 1;
        mp_Child = child;
        mp_Parent = 0;
        generateID();
    }

    Object::Object(Object* child, Object* parent, float mass)
    :   position(0, 0)
    {
        mf_Mass = mass;
        mf_Volume = 1;
        mp_Child = child;
        mp_Parent = parent;
        generateID();
    }

    Object::Object(Object* child, float mass, float volume)
    :   position(0, 0)
    {
        mf_Mass = mass;
        mf_Volume = volume;
        mp_Child = child;
        mp_Parent = 0;
        generateID();
    }


//Object Constructors with 4 known field(s)
    Object::Object(Point point, Object* child, Object* parent, float mass)
    :   position(point)
    {
        mf_Mass = mass;
        mf_Volume = 1;
        mp_Child = child;
        mp_Parent = parent;
        generateID();
    }

    Object::Object(Object* child, Object* parent, float mass, float volume)
    :   position(0, 0)
    {
        mf_Mass = mass;
        mf_Volume = volume;
        mp_Child = child;
        mp_Parent = parent;
        generateID();
    }

//Object Constructors with 5 known field(s)
    Object::Object(Point point, Object* child, Object* parent, float mass, float volume)
    :   position(point)
    {
        mf_Mass = mass;
        mf_Volume = volume;
        mp_Child = child;
        mp_Parent = parent;
        generateID();
    }

//Object Interaction Methods
    void Object::move(int x = 0, int y = 0)
    {
        position.move(x, y);
    }

    void Object::move(Point point)
    {
        position.move(point);
    }

    void Object::destroyChild()
    {
        if(mp_Child)
        {
            mp_Child->~Object();
            resetChild();
        }
        else
        {
        resetChild();
        }
    }
    void Object::resetChild()
    {
        mp_Child = 0;
        mb_Parent = false;
    }

    void Object::resetParent()
    {
        mp_Parent = 0;
        mb_Child = false;
    }

    float Object::calculateDensity()
    {
        return (mf_Mass / mf_Volume);
    }

Point.h:

class Point {
        public:
        int _X;
        int _Y;

        Point()
        {
            _X = 0;
            _Y = 0;
        }

        Point(int x)
        {
            _X = x;
            _Y = 0;
        }

        Point(int x, int y)
        {
            _X = x;
            _Y = y;
        }

        void reset()
        {
            _X = 0;
            _Y = 0;
        }

        void moveX(int x)
        {
            _X = x;
        }

        void moveY(int y)
        {
            _Y = y;
        }

        void move(int x, int y)
        {
            _X = x;
            _Y = y;
        }  

        void move(Point point)
        {
            _X = point._X;
            _Y = point._Y;
        }

        void offsetX(int osX)
        {
            _X += osX;
        }

        void offsetY(int osY)
        {
            _Y += osY;
        }

        void offset(int osX, int osY)
        {
            _X += osX;
            _Y += osY;
        }
    }


Main.cpp的:

#include "Object.h"
#include "Point.h"
#include <iostream>

using namespace std;

int main()
{
    Point origin;
    Point p1(1);
    Point p2(1, 3);

    Object obj1();

    Object* child;
    child = &obj1;

    Object obj2(origin);
    Object obj3(&obj2);
    Object obj4(1.25);

    Object obj5(p1, &obj1);
    Object obj6(p1, 4.0f);
    Object obj7(&obj1, 4.0f);
    Object obj8(&obj2, &obj3);
    Object obj9(2.0f, 4.0f);

    Object obj10(p2, &obj1, &obj2);
    Object obj11(p2, &obj1, 4.0f);
    Object obj12(&obj1, &obj2, 3.0f);
    Object obj13(&obj1, 2.5f, 2.6f);

    Object obj14(origin, &obj10, &obj11, 10.0f);
    Object obj15(&obj12, &obj13, 11.0f, 13.0f);

    Object obj16(p1, &obj14, &obj15, 100.0f, 100.0f);

    Object objs[] = { obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12, obj13, obj14, obj15, obj16 };

    for (int i = 0; i < 16; i++)
    {
        cout << objs[i];
    }
    system("pause");
    return 0;
}


請注意:我知道我還沒有重載operator <<函數,這即將到來。 我想先解決其他問題,然后再嘗試進行任何操作。
另外,我知道“ system(“ pause”);“ 我們不建議使用line並皺眉,這只是一個個人項目,所以我現在不在乎它是特定於OS的,如果決定移植到另一個平台,我將對其進行更改。

我的語法或邏輯沒有發現任何問題,但是,我對C ++類很陌生,所以我願意接受任何建議,謝謝!!!

這個:

Object obj1();

是一個函數聲明。 這是一個名為obj1的函數,該函數接受零個參數並返回一個Object 這就是所謂的“最煩人的解析”。 您的意思是:

Object obj1;

您可以從錯誤消息中看出來,表明您的右側類型為“指向函數的指針,該函數接受零參數並返回一個Object而不是您期望的” Object指針”:

錯誤:類型“ Object (*)() ”的值不能用於初始化“ Object * ”類型的實體

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM