请看下面的代码
位置.h
#pragma once
class Location
{
public:
Location(void);
Location(int,int,int);
~Location(void);
Location(const Location *loc);
void display();
void set(int,int,int);
private:
int x,y,z;
};
位置.cpp
#include "Location.h"
#include <iostream>
#include <string>
using namespace std;
Location::Location()
{
}
Location::Location(int x,int y, int z)
{
set(x,y,z);
}
Location::~Location(void)
{
}
void Location::display()
{
cout << "X: " << x << endl;
cout << "Y: " << y << endl;
cout << "Z: " << z << endl;
}
void Location::set(int xx,int yy,int zz)
{
x = xx;
y = yy;
z = zz;
}
对象.h
#pragma once
#include "Location.h"
class GameObject
{
public:
GameObject(int);
~GameObject(void);
GameObject(int,Location *);
GameObject(const GameObject *obj);
Location *location;
int getNumberOfObjects();
static int counter;
int id;
private:
GameObject(void);
};
对象.cpp
#include "GameObject.h"
#include <iostream>
using namespace std;
static int counter = 0;
int GameObject::counter = 0;
GameObject::GameObject(void)
{
}
GameObject::GameObject(int i)
{
counter++;
id = i;
}
GameObject::GameObject(int i,Location *loc)
{
id = i;
location = loc;
}
GameObject::GameObject(const GameObject *ob)
{
this->location = new Location(ob->location);
}
GameObject::~GameObject(void)
{
}
主文件
#include <iostream>
#include "GameObject.h"
using namespace std;
int main()
{
//GameObject obj1;
//cout << obj1.id << endl;
GameObject obj2(45);
cout << obj2.id << endl;;
// cout << obj2.counter << endl;
//Declaring dynamic objects for Location
Location *loc1 = new Location(1,1,1);
Location *loc2 = new Location(2,2,2);
Location *loc3 = new Location(3,3,3);
//Assigning each GameObject a location
GameObject obj3(45,loc1);
GameObject obj4(45,loc2);
GameObject obj5(45,loc3);
//Displaying Number of GameObject objects
cout << "Number of Objects: " << GameObject::counter << endl;
//Invoking Location's display() method using GameObject objects
cout << "......obj3 values........" << endl;
obj3.location->display();
cout << "......obj4 values........" << endl;
obj4.location->display();
cout << "......obj5 values........" << endl;
obj5.location->display();
//Declaring new Static GameObject
GameObject obj6(obj4);
//Invoking display() member function using both obj4 and obj6 GameObjects
cout << endl;
cout << "Invoking display() member function using both obj4 and obj6 GameObjects " << endl;
obj4.location->display();
obj6.location->display();
//Changing the location values in obj4
obj4.location->set(8,8,8);
//Invoking display() member function using both obj4 and obj6 GameObjects
cout << endl;
cout << "Invoking display() member function using both obj4 and obj6 GameObjects " << endl;
obj4.location->display();
obj6.location->display();
system("pause");
return 0;
}
在这里,正如您在 Main.cpp 中看到的,obj6 'Location' 已在 obj4 'Location' 更改后立即更改。当我更改 obj4 的位置时,我不想更改 obj6 的“位置”。
执行此操作时出现此错误
GameObject.obj : error LNK2019: unresolved external symbol "public: __thiscall Location::Location(class Location const *)" (??0Location@@QAE@PBV0@@Z) referenced in function "public: __thiscall GameObject::GameObject(class GameObject const *)" (??0GameObject@@QAE@PBV0@@Z)
我试图用一个深拷贝构造函数来做,但它没有用。请帮忙。