[英]Undefined reference errors when using Netbeans (C++) with G++
I'm working on this since hours and I believe I overlooked something. 我几个小时以来一直在努力,我相信我忽略了一些东西。 I'm new to C++ and programming in general. 我是C ++和一般编程的新手。 I'm trying to build a program which consists of several .cpp-files. 我正在尝试构建一个包含几个.cpp文件的程序。 The associated headerfiles are in an include-directory. 关联的头文件位于包含目录中。 I've of course also told netbeans where to search for them (Project >> Properties >> build >> C++-Compiler >> include directories). 我当然也告诉netbeans在哪里搜索它们(项目>>属性>>构建>> C ++编译器>>包含目录)。
That's the main-file: 那是主文件:
//main.cpp
#include "Eingabe_Konstanten.h"
#include "Stoffwerte.h"
#include "Zustandsgroessen.h"
#include <iostream>
using namespace std;
int main()
{
Eingabe_Konstanten ein;
cout << ein.Pi();
cin.get();
}
And that's the associated headerfile "Eingabe_Konstanten.h" (sorry, it's partly german... ;)) 这就是相关的头文件“ Eingabe_Konstanten.h”(对不起,它部分是德语...;)
//Eingabe_Konstanten.h
#ifndef EINGABE_KONSTANTEN_H
#define EINGABE_KONSTANTEN_H
class Eingabe_Konstanten {
public:
double Pi();
double mStrom();
};
#endif /* EINGABE_KONSTANTEN_H */
With the corresponding .cpp-file 与相应的.cpp文件
//Eingabe_Konstanten.cpp
#include "Eingabe_Konstanten.h"
#include "InputOutput.h"
#include <string>
InputOutput io;
double Pi()
{
double pi = M_PI;
return pi;
}
double mStrom()
{
double m = io.lese(7);
return m;
}
lese()
is a method which reads lines from a file. lese()
是一种从文件读取行的方法。 It's in an InputOutput Class. 它在InputOutput类中。
//InputOutput.h
#ifndef INPUTOUTPUT_H
#define INPUTOUTPUT_H
#include <string>
class InputOutput {
public:
double lese(int);
};
#endif /* INPUTOUTPUT_H */
*************************************
//InputOutput.cpp
#include "InputOutput.h"
#include <iostream>
#include <string>
#include <sstream>
#include <limits>
#include <fstream>
#include <stdlib.h>
using namespace std;
double lese(int zeile)
{
ifstream datei("input.txt");
if(!datei.is_open())
{
cerr << "Fehler beim Oeffnen der Datei" << endl;
return 0;
}
for(; zeile > 1; --zeile)
datei.ignore(numeric_limits<streamsize>::max(), '\n');
string input;
if(!getline(datei, input))
{
cerr << "Fehler beim Lesen aus der Datei" << endl;
return 0;
}
double wert = atof(input.c_str());
return wert;
}
I think it's a linking problem. 我认为这是一个链接问题。 Because the object files don't seem to be linked correctly together and the compiler doesn't complain until the linking process. 因为目标文件似乎没有正确地链接在一起,并且编译器在链接过程之前不会抱怨。 The output looks like this: 输出看起来像这样:
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/Maschinenbau/Diplom/Diplomarbeit/Programm/Comp_Pred'
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_TDM-Windows/comp_pred.exe
make.exe[2]: Entering directory `/d/Maschinenbau/Diplom/Diplomarbeit/Programm/Comp_Pred'
mkdir -p dist/Debug/MinGW_TDM-Windows
g++ -m32 -o dist/Debug/MinGW_TDM-Windows/comp_pred build/Debug/MinGW_TDM-Windows/_ext/725510466/Eingabe_Konstanten.o build/Debug/MinGW_TDM-Windows/_ext/725510466/InputOutput.o build/Debug/MinGW_TDM-Windows/_ext/725510466/Stoffwerte.o build/Debug/MinGW_TDM-Windows/_ext/725510466/Zustandsgroessen.o build/Debug/MinGW_TDM-Windows/main.o -Llib -lCoolProp
build/Debug/MinGW_TDM-Windows/_ext/725510466/Eingabe_Konstanten.o: In function `Z6mStromv':
d:/Maschinenbau/Diplom/Diplomarbeit/Programm/Comp_Pred/Eingabe_Konstanten.cpp:24: undefined reference to `InputOutput::lese(int)'
I'm sorry for this comprehensive description, but I really don't know what to do. 抱歉,我对此内容进行了全面介绍,但我真的不知道该怎么办。 I would be very grateful for any help. 我将非常感谢您的帮助。
You have defined your lese()
function as a freestanding function, but you need it to be a member of your InputOutput
class. 您已经将lese()
函数定义为独立函数,但是您需要它成为InputOutput
类的成员。 So 所以
double lese(int zeile)
should be 应该
double InputOutput::lese(int zeile)
The same goes for the functions of your Eingabe_Konstanten
class Eingabe_Konstanten
类的功能也是Eingabe_Konstanten
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.