c++主文件混乱

c++ main file confusion

本文关键字:混乱 主文件 c++      更新时间:2023-10-16

作为我大学作业的一个小项目,我想到了c++。

我的项目有很多*.h和相应的*.cpp文件。

然后我创建了一个具有main函数的主文件。我在main中包含了我需要的所有头文件。但它表明[链接错误]对Class_Name::Class_Constructor()的未定义引用也可用于其他功能

然后我用源cpp文件替换了头文件,它工作了。现在我想应该是另一种方式。和无法解释为什么会发生。,因为我们通常只包含头文件,这是可行的。通常我们只需要声明,现在我们需要main中的定义。

//请注意,下面的代码是工作的,但我想包括头文件,而不是源文件。

代码如下://我的主文件

#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

/*这是注册类。*/

#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)

清洁:rm *。x * . ocd注册rm *。x * . o

rebuild: clean build

----------

现在的问题是:

我的模板参数总是int ..这意味着我应该做template_Class<16>;在CPP文件中。

如果你看到我的代码。我可以在运行时获得regSize,因为我想在我的RegisterClass中获得不同大小的寄存器,具有类型变量bitset .

如果我只能在编译时这样做,那么我应该做#define regSize 16而不是制作模板。

或者我应该把所有的代码只放在模板文件中

您应该只在源代码中包含头文件。但是,您必须编译并链接项目中的所有.cpp文件。如果不这样做,编译器/链接器将无法找到头文件中定义的函数的实现。


看到源代码后,我认为问题是在makefile。改变这个:

.cpp.o: 
$(CC) -c $< 

:

.o: 
$(CC) -c $< 
  • 如果"main.cpp"是你编译的唯一源文件,那么这应该可以正常工作:
<>之前//实际上,您已经为整个项目创建了一个大而愉快的源文件# include"MicroProcessor.cpp"# include"注册/Register.cpp"# include"注册/RegistersName.cpp"# include使用命名空间std;int main () {微处理器mp;mp。move(RegistersName::AX, RegistersName::BX);cout 之前
  • 另一方面,如果你编译"main。o"(与#included'模块)和"微处理器。cpp"和…,那么当你创建。exe

  • 时,你应该"复制定义"链接错误。
  • 我的猜测是,你的构建工具未能找到(或只是未能构建)其他模块,因此"未定义的引用"链接错误。因此,你的"#includes"没有冲突。

您正在使用哪个编译器/IDE来构建代码?检查具有Class_Name::Class_Constructor的源文件,并在构建过程中添加相同的源文件。