我一直在爲uni和空閒時間做C編程,我正在轉向使用C++,並試圖從單獨的文件和類中開始,但是我遇到了一些問題當試圖做我的項目。我使用下面的Makefile未定義的引用class :: function
EXEC = main
OBJS = main.o Bunny.o Name.o Board.o
CC = g++
CFLAGS = -Wall -Wextra -Werror
LIBS =
LDFLAGS =
$(EXEC): $(OBJS)
$(CC) $(LDFLAGS) -o [email protected] $(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是:
[email protected]:~/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
目前,我只是想看看我是否能在不同的文件,因此不完全使用的方法從一個類碼。
對不起,關於牆的文字,但我很好,真的難住,所以如果任何人可以提供任何意見,將不勝感激。
不相關的問題,但...你想'CXX = G +'和'CXXFLAGS == -Wall -Wextra -Werror'在你的makefile。 –