对 class::function 的未定义引用
Undefined reference to class::function
我一直在为 uni 做 C 编程,在业余时间,我正在转向 c++,并尝试从单独文件中的类开始,但是我在尝试制作项目时遇到了一些问题。我正在使用以下生成文件
EXEC = main
OBJS = main.o Bunny.o Name.o Board.o
CC = g++
CFLAGS = -Wall -Wextra -Werror
LIBS =
LDFLAGS =
$(EXEC): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
.PHONY: clean
clean:
rm -f $(EXEC) $(OBJS)
run:
./$(EXEC)
这只是我的 C 的更改版本,所以可能是这样,这是相关的其他文件
主.cpp
#include <iostream>
#include "Name.hpp"
#include "definitions.hpp"
int main() {
Name nameContainer;
nameContainer.initMaleNameValidArray();
std::cout << "Got to just before the return!n" << std::endl;
return 0;
}
名称.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "definitions.hpp"
class Name{
public:
void initFemaleNameArray() {
std::ifstream myReadFile;
myReadFile.open("girlsnames.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
std::cout<<output;
}
}
myReadFile.close();
}
void initMaleNameArray() {
}
void initFemaleNameValidArray() {
static int femaleValidArray[GIRLS_NAMES] = {0};
}
void initMaleNameValidArray() {
static int MaleValidArray[GIRLS_NAMES] = {0};
}
void freeAFemaleName(std::string name);
void freeAMaleName(std::string name);
std::string returnFreeFemaleName();
std::string returnFreeMaleName();
private:
std::string femaleNameArray[GIRLS_NAMES];
std::string maleNameArray[BOYS_NAMES];
int femaleValidArray[GIRLS_NAMES];
int maleValidArray[BOYS_NAMES];
};
名称.hpp
#ifndef NAME_HPP
#define NAME_HPP
#include "definitions.hpp"
class Name{
public:
void initFemaleNameArray();
void initMaleNameArray();
void initFemaleNameValidArray();
void initMaleNameValidArray();
void freeAFemaleName(std::string name);
void freeAMaleName(std::string name);
std::string returnFreeFemaleName();
std::string returnFreeMaleName();
private:
std::string femaleNameArray[GIRLS_NAMES];
std::string maleNameArray[BOYS_NAMES];
int femaleValidArray[GIRLS_NAMES];
int maleValidArray[BOYS_NAMES];
};
#endif
运行make时出现的错误是:
mark@Mark-Linux:~/Documents/Misc/C++/Bunny$ make
g++ -c -o main.o main.cpp
g++ -c -o Bunny.o Bunny.cpp
g++ -c -o Name.o Name.cpp
g++ -c -o Board.o Board.cpp
g++ -o main main.o Bunny.o Name.o Board.o
main.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `Name::initFemaleNameArray()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
目前,我只是想看看是否可以使用不同文件中类的方法,因此代码不完整。
对不起,文字墙,但我很好,真的很难,所以如果有人可以提供任何建议,将不胜感激。
在name.hpp中,你定义了Name类,这没关系。但在名义上.cpp您再次定义了该类。这不是你想要的。你应该有名称.cpp实现函数。那看起来像这样:
#include <iostream>
#include <fstream>
#include <string>
#include "definitions.hpp"
#include "name.hpp"
void Name::initFemaleNameArray() {
std::ifstream myReadFile;
myReadFile.open("girlsnames.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
std::cout<<output;
}
}
myReadFile.close();
}
void Name::initMaleNameArray() {}
void Name::initFemaleNameValidArray() {
// This is wrong, too. This creates a new array
//static int femaleValidArray[GIRLS_NAMES] = {0};
// You want this:
memset(femaleValidArray, 0, sizeof(femaleValidArray));
}
void Name::initMaleNameValidArray() {
//static int MaleValidArray[GIRLS_NAMES] = {0};
memset(MaleValidArray, 0, sizeof(MaleValidArray));
}
void Name::freeAFemaleName(std::string name) {}
void Name::freeAMaleName(std::string name) {}
std::string Name::returnFreeFemaleName() {}
std::string Name::returnFreeMaleName() {}
Your Name.cpp 和 Name.hpp 实际上是在声明单独的类,这可能是未定义的行为。相反,您的姓名.cpp应如下所示:
#include <iostream>
#include <fstream>
#include <string>
#include "Name.hpp"
void Name::initFemaleNameArray() {
std::ifstream myReadFile;
等。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用