As making a small project for my college work, i thought to work on c++.
My project has many *.h and corresponding *.cpp files.
Then i created a main file that has the main function. I included all the header files that i required in main. but it showed [Linker error] undefined reference to `Class_Name::Class_Constructor()' and also for other functions.
Then i replaced header files with source cpp files and it worked. Now i thought that it should be other way round. and cannot explain why it is happening. since we generally include header files only and that works. May be generally we require only declarations and now we require definitions in main. Am i right ?
// please note that the following code is working but i want to include header files not source files.
Here is the code: // My Main file
#include "MicroProcessor.cpp"
#include "Register/Register.cpp"
#include "Register/RegistersName.cpp"
#include <iostream>
using namespace std;
int main(){
MicroProcessor<REG_SIZE> mp;
mp.move(RegistersName::AX , RegistersName::BX);
cout << "hello";
return 0;
}
#ifndef MICROPROCESSOR_H
#define MICROPROCESSOR_H
#include "Register/RegistersName.h"
#include "Register/Register.h"
#include "Register/RegisterConstants.h"
using namespace std;
template<int regSize>
class MicroProcessor
{
private:
Register<REG_SIZE> *registers;
Register<FLAG_SIZE> *flags;
public:
MicroProcessor();
~MicroProcessor();
void move(RegistersName destination, RegistersName source);
};
#endif // MICROPROCESSOR_H
#ifndef REGISTER_H
#define REGISTER_H
/* This is the Register Class. */
#include <bitset>
#include "RegisterConstants.h"
using namespace std;
template <int regSize=REG_SIZE>
class Register
{
private:
bitset<regSize> reg ;
int carry ;
int a;
public:
// Constructor
Register();
// parameterized constructor
// @param:
//
Register(const string &binary);
// destructor
// get the bitset<> reg bit number position
int get(int position)const;
//reset the bit at position
void reset(int position);
//reset the bit at position
void set(int position);
// move the value of a register into another
Register move(const Register &source, int startIndex=0, int len=REG_SIZE);
// display the contents of the register but from last to first
// because lowest bit is stimulated as 0th index of register in memory.
void display();
// return the size of the register bitset<> reg
int size()const;
// add two registers result = this + other, return result
// but should be this = this + other, return this
// this is more like operator +()
Register add(const Register &other);
// adds two registers and returns the sum of the registers without modifying any of them
Register operator+(const Register &other);
// add the bits of the register
int addBits(int bit1, int bit2, int cary);
// is carry is set after the operations
bool isCarry();
};
#endif // REGISTER_H
#ifndef REGISTER_CPP
#define REGISTER_CPP
#include <iostream>
using namespace std;
#include "Register.h"
#include <bitset>
// Constructor
template<int regSize> Register<regSize> :: Register(){
carry = 0;
}
/* other code */
#endif // REGISTER_CPP
CC = g++
HEADER = Register/Register.h Register/RegistersName.h MicroProcessor.h
SRC = Register/Register.cpp Register/RegistersName.cpp MicroProcessorTest.cpp
MicroProcessor.cpp
OUTPUT = out_executable.x
OBJS = MicroProcessorTest.o MicroProcessor.o Register/Register.o
Register/RegistersName.o
.cpp.o:
$(CC) -c $<
MicroProcessorTest.o: MicroProcessorTest.cpp MicroProcessor.h Register/Register.h
Register/RegistersName.h
MicroProcessor.o: MicroProcessor.cpp Register/Register.h Register/RegistersName.h
Register/Register.o: Register/Register.cpp Register/RegistersName.h
Register/RegistersName.o: Register/RegistersName.cpp
build: $(OBJS)
$(CC) -o $(OUTPUT) $(OBJS)
./$(OUTPUT)
clean: -rm *.x *.o cd Register -rm *.x *.o
rebuild: clean build
----------
well now the problem is:
well the template parameter of mine is always int .. so should that means that i should do something like template_Class<16> ; in cpp file.
well if you see my code. can i get regSize at run time because that what i want to get different size of registers in my RegisterClass that has a varible of type bitset.
if i can only do that at compile time than i should be doing #define regSize 16 rather than making a template.
or should i put all the code in the template file only