0

我一直在为 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 的一个修改版本,所以可能是这样,这里是相关的其他文件

主文件

#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

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

对文字墙感到抱歉,但我真的很困惑,所以如果有人能提供任何建议,将不胜感激。

4

2 回答 2

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() {}
于 2012-10-16T12:55:00.113 回答
1

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

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

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

等等

于 2012-10-16T12:52:42.933 回答