对 class::function 的未定义引用

Undefined reference to class::function

本文关键字:未定义 引用 function class      更新时间:2023-10-16

我一直在为 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;

等。