c++主文件混乱
c++ main file confusion
作为我大学作业的一个小项目,我想到了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"是你编译的唯一源文件,那么这应该可以正常工作:
另一方面,如果你编译"main。o"(与#included'模块)和"微处理器。cpp"和…,那么当你创建。exe
时,你应该"复制定义"链接错误。我的猜测是,你的构建工具未能找到(或只是未能构建)其他模块,因此"未定义的引用"链接错误。因此,你的"#includes"没有冲突。
您正在使用哪个编译器/IDE来构建代码?检查具有Class_Name::Class_Constructor
的源文件,并在构建过程中添加相同的源文件。
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- VI工作室代码错误无法打开输出文件主.exe
- 如何正确定义C++类析构函数并将其链接到主文件?
- 头文件可以在主类中运行.这怎么可能?
- 如何从主.cpp中的cpp文件中获取功能结果?
- 链接外部C++外部C++ 在主文件中?
- 为什么我的主文件.cpp不打印头文件中的任何内容
- cpp 在主源文件中包括.cpp文件导致错误"duplicate symbol"
- 重定向输入和输出时如何处理主文件I/O?
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 如何将 Cython 生成的模块从 python 导入到 C/C++ 主文件?(C/C++编程)
- 标题和INT主文件
- 关于 Linux .so 文件无法链接到主文件.cpp文件
- 如何在单个 Visual Studio 2010 项目中执行多个主文件
- 在C++中使用结构的头文件、初始化文件和主文件
- 从文件中读取矩阵,将其提供给主文件
- 如何使用 C++ 读取 NTFS 主文件表
- 如何在单独的cpp文件中通过头文件调用函数到主文件
- 将程序拆分为头文件和主文件
- 我的主C++文件中的文本(在尚未执行的代码中)如何显示在字符串中?