我试图在名为“ world”的类中创建对象“ Person”的实例。
但是,每当我尝试构建项目时,都会出现以下错误:
g++ main.cpp world.cpp Colony.cpp Person.cpp -lsfml-graphics -lsfml-window -lsfml-system -o run
Undefined symbols for architecture x86_64:
"Person::Person(int, int, bool, float, sf::Color)", referenced from:
world::populate(int, std::__1::uniform_int_distribution<float>&, std::__1::uniform_int_distribution<float>&, std::__1::linear_congruential_engine<unsigned int, 48271u, 0u, 2147483647u>&) in world-6df84d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [make] Error 1
我创建此实例的部分如下:
Person p(0, 0, false, 5.0f, sf::Color::Green);
我的world.cpp文件的顶部包含以下内容
#include <random>
#include <math.h>
#include "world.h"
#include "Person.h"
world::world(sf::Sprite mapSpr, std::string& mapRef)
{
worldMap = mapSpr;
if(!mapImage.loadFromFile(mapRef)){
std::cout << "Error; could not load map image" << std::endl;
}
}
我的Person.h文件如下:
#include <SFML/Graphics.hpp>
#include <math.h>
#include <random>
#ifndef __PERSON_H__
#define __PERSON_H__
class Person {
private:
int x, y;
bool hasDisease;
float strength;
float reproVal;
sf::Color color;
sf::VertexArray points;
int vectPos; //Keeps track of position of person in colony vector so removal is O(1)
public:
Person(int posX, int posY, bool disease, float str, sf::Color col);
int getX();
int getY();
bool getDisease();
float getStrength();
float getReproVal();
int getVectPos();
//Setters
void setDisease();
void setX(int dx);
void setY(int dy);
void setColor(sf::Color nColor);
void setVectPos(int vPos);
void move(sf::Sprite& map, std::uniform_int_distribution<int>& distrib, std::default_random_engine& generator);
};
#endif
最后,我的Person.cpp构造函数看起来像:
#include <SFML/Graphics.hpp>
#include <math.h>
#include <random>
#include "Person.h"
inline Person::Person(int posX, int posY, bool disease, float str, sf::Color col){
x = posX;
y = posY;
hasDisease = disease;
strength = str;
reproVal = 0;
color = col;
points = sf::VertexArray(sf::Points, 1);
}
我尝试做
Person::Person p(0, 0, fale, 5.0f, sf::Color::Green);
但这导致了相同的错误。
我对C ++还是很陌生,我想我不太了解使对象以这种语言工作的方式。