对class :: function的未定义引用

时间:2012-10-16 12:33:32

标签: c++ class reference undefined

我一直在为uni做C编程,在我的业余时间我正在转向c ++并且我试图从单独的文件中开始编程等等,但是在尝试创建项目时我遇到了一些问题。我正在使用以下makefile

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版本的改进版本所以可能是这样,这里是相关的其他文件

的main.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;
}

Name.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];
};

Name.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

目前我只是想看看我是否可以在不同文件中使用类中的方法,因此代码不完整。

对于文字墙感到抱歉,但我很好并且真的很难过,所以如果有人能提出任何值得赞赏的建议。

2 个答案:

答案 0 :(得分:3)

在name.hpp中,您正在定义Name类,这没关系。但是在name.cpp中,你再次定义了这个类。这不是你想要的。你应该让name.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() {}

答案 1 :(得分:1)

您的Name.cpp和Name.hpp实际上是声明了单独的类,这可能是未定义的行为。相反,你的Name.cpp应如下所示:

#include <iostream>
#include <fstream>
#include <string>
#include "Name.hpp"

void Name::initFemaleNameArray() {
    std::ifstream myReadFile;