I've been doing C programming for uni and in my spare time I'm moving onto c++ and am trying to start with classes in seperate files and whatnot, but I'm getting some problems when trying to make my project. I'm using the following 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)
Which is just an altered version of my C one so it might be that, and here are the relevant other files
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
And the error im getting when running make is:
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
At the moment I'm just trying to see if I can use a method from a class in a different file hence the incompleteness of the code.
Sorry about the wall of text but I'm well and truly stumped, so if anyone could offer any advice that'd be greatly appreciated.
CXX=g++andCXXFLAGS== -Wall -Wextra -Werrorin your makefile.