我正在处理三个项目,一个库,主要项目是一个C++控制台应用程序,以及一个单元测试项目。当在主应用程序和测试中使用my.lib中的函数时,一切都很正常。然而,如果我尝试使用自定义运算符,它在主应用程序中不起作用,并且我得到一个LNK2019未解析的外部符号。单元测试项目可以很好地使用这些操作符。有人知道问题出在哪里吗?
我在库中的头文件如下:
#pragma once
#include <iostream>
namespace Core
{
class Vector3
{
private:
float m_coordinates[3];
public:
Vector3();
Vector3(float x, float y, float z);
~Vector3();
float getX();
void setX(float value);
float getY();
void setY(float value);
float getZ();
void setZ(float value);
Vector3 operator= (Vector3& other);
float operator[] (unsigned int index) const;
bool operator==(const Vector3& other) const;
Vector3 operator+(const Vector3& other) const;
Vector3 operator-(const Vector3& other) const;
friend std::ostream& operator<<(std::ostream& os, const Vector3& obj);
};
}
在主应用程序中:
Core::Vector3 someVector;
Core::Vector3 anotherOne(1, 2, 3);
someVector == anotherOne; //works
std::cout << anotherOne << std::endl; //LNK2019 unresolved external symbol
。cpp文件
std::ostream& operator<<(std::ostream& os, const Vector3& obj) {
os << "{" << obj[0] << "," << obj[1] << "," << obj[2] << "}";
return os;
}
编辑:当我用()初始化时,==不能工作。该<<;还是不起作用
someVector == anotherOne
你是说
someVector = anotherOne