什么是未定义的参考/未解决的外部符号错误?什么是常见原因以及如何修复/预防它们?
随意编辑/添加您自己的.
c++ c++-faq linker-errors unresolved-external undefined-reference
我遇到的问题是,当我调用我创建的类的构造函数时,我得到以下错误.
main.cpp:20:未定义引用`StaticObject :: StaticObject(Graphics*,sf :: String,
sf :: Vector2)'
这个问题可以"修复",在main.cpp中为.cpp文件添加一个include,就像这样.
...
#include "GameObjects/StaticObject.cpp"
...
Run Code Online (Sandbox Code Playgroud)
虽然这解决了这个问题,但这似乎是一个糟糕的解决方案,并且违背了我之前所说的.有没有其他方法可以解决这个问题?我正在使用带有g ++的Netbeans 7.3来编写/编译这个程序.以下是相关代码.
main.cpp中
...
#include <SFML/Graphics.hpp>
#include "Graphics/Graphics.hpp"
#include "GameObjects/StaticObject.hpp"
int main(int argc, char** argv) {
//SETUP
Graphics graphics;
background = new StaticObject(&graphics, "Data/Images/BackgroundPlaceholder.png", sf::Vector2f(0,0));
...
Run Code Online (Sandbox Code Playgroud)
main.hpp
...
#include <SFML/Graphics.hpp>
#include "GameObjects/StaticObject.hpp"
...
// Objects
StaticObject *background;
...
Run Code Online (Sandbox Code Playgroud)
StaticObject.hpp
#include <SFML/Graphics.hpp>
#include "../Graphics/Graphics.hpp"
class StaticObject{
public:
StaticObject();
StaticObject(Graphics *_graphics, sf::String texture_filename, sf::Vector2f _position);
StaticObject(const StaticObject& orig);
virtual ~StaticObject();
private:
// The sprite stores the position …Run Code Online (Sandbox Code Playgroud) 我遇到了项目的框架和参考选项卡,发现我可以"添加新参考..."到我的项目,这是什么功能?
我收到标题中所述的错误。我确保以下各项:
-正确设置了Include目录,include库和其他include目录
-在属性中,Subsystem设置为CONSOLE
对我的代码的注释:LifeLib是一个项目,其中包含我要测试某些方法的类。这些类在命名空间LifeLib中定义。StornoTafel就是其中之一。在任何名称空间中均未定义testVariables。
对于StornoTafel中的2个构造函数和1个方法,我得到了3次链接错误(代码中已指出)。
//project Tester
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../LifeLib/StornoTafel.h"
#include "../LifeLib/testVariables.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Tester
{
TEST_CLASS(AggSelTest)
{
public:
LifeLib::StornoTafel stornoTafel_; // LNK2019
LifeLib::StornoTafel *stornoTafel_; // no error, but I need an instance and not a reference to proceed -> see init method
LifeLib::testVariables test_vars_; // everything is fine
TEST_METHOD_INITIALIZE(init) {
stornoTafel_ = StornoTafel(test_vars_.lapseProb); // when this line is commented out I only get the first error (see below)
}
}
}
// testVariables.h
#pragma …Run Code Online (Sandbox Code Playgroud)