如何防止重新定义类?
How to prevent Redefinition of class?
我一直在从最新的C++书中实现一个自定义向量,我在类向量和 Vector::Vector 的 vector.cpp 和 vector.h 文件中遇到了错误: "向量"的重新定义
以下是每个文件的代码:
主.cpp:
#include <iostream>
#include "Vector.h"
#include "Container.h"
int main(int argc, const char * argv[]) {
return 0;
}
矢量.h
class Vector {
public:
Vector(int s) :elem{new double[s]}, sz{s} // constructor: acquire resources
{
for (int i=0; i!=s; ++i) // initialize elements
elem[i]=0;
}
~Vector() { delete[] elem; } // destructor: release resources
double& operator[](int i);
int size() const;
private:
double* elem; // elem points to an array of sz doubles
int sz;
};
矢量.cpp
Vector::Vector(int s) // definition of the constructor
:elem{new double[s]}, sz{s} // initialize members
{}
double& Vector::operator[](int i) // definition of subscripting
{
return elem[i];
}
int Vector::size() const // definition of size()
{
return sz;
}
容器.cpp
class Container {
public:
virtual double& operator[](int) = 0; // pure virtual function
virtual int size() const = 0; // const member function (§4.2.1)
virtual ~Container() {} // destructor (§4.2.2)
};
class Vector_container : public Container { // Vector_container implements Container
public:
Vector_container(int s) : v(s) { } // Vector of s elements
~Vector_container() {}
double& operator[](int i) override { return v[i]; }
int size() const override { return v.size(); }
private:
Vector v;
};
我尝试用 #ifndef Vector_h,#define Vector_h 和 #endif 将 .h 文件括起来,但错误仍然出现在Vector_cpp中。如果我用 #ifndef Vector_cpp、#define Vector_h 封装 Vector.cpp,#endif 仍然会出现错误。
雪上加霜的是,当我将 Vector.h 添加到容器中时.cpp它告诉我:我声明 Vector v 的底部有未知类型名称"Vector">
谁能帮我解决这个问题?
您还需要为 Container 类实现一个头文件,因此您也需要一个Container.h
(请注意,这应该与文件名匹配(。在您的情况下,似乎只需将文件从 Container.cpp 重命名为 Container.h 就足够了。
此外,您应该将方法(例如Vector
的构造函数(实现到一个文件中,而不是两个文件中。例如,您可以在头文件中声明该方法,然后在源文件中定义它。
将所有内容放在一起,您需要:
- 主.cpp(此处没有变化(
- Container.h(而不是 Container.cpp,但内容没有变化 文件,你做得很好!:))
-
Vector.h
class Vector { public: Vector(int s); ~Vector(); double& operator[](int i); int size() const; private: double* elem; // elem points to an array of sz doubles int sz; };
-
矢量.cpp
#include "Vector.h" Vector::Vector(int s) :elem{new double[s]}, sz{s} // initialize members {} double& Vector::operator[](int i) // definition of subscripting { return elem[i]; } int Vector::size() const // definition of size() { return sz; } Vector::~Vector() { delete[] elem; }
编译:
g++ main.cpp Vector.cpp
- .常见的做法是遵循下面的项目结构(使用防护指令(:
类名1.h
#ifndef CLASSNAME1_H
#define CLASSNAME1_H
// class definition
class ClassName1
{
// ...
};
#endif
类名1.cpp
#include "ClassName1.h"
// implementation of ClassName1 functions may go here - except templates - templates should be defined eventually in one header file (not separate compilation unit) or one header with template body may include implementation from second header with implementation if template is big enough- extension , say - ipp. At least this is limitation for C++98.
ClassName1::someFunction()
{
// ...
}
主.cpp//或其他一些使用 ClassName1 的用户代码
#include "ClassName1.h"
ClassName1::someStaticFunction(); // for instance
ClassName1 className1; // for instance
对所有类系统地遵循此结构,您不会遇到类重新定义。 这使得代码具有可移植性。
2.还有另一种非标准方式(Visual C++,GCC( - 使用:#pragma 一次指令
#pragma once
struct foo {
int member;
};
- 如何防止重新定义类?
- 在使用Qt Designer Form Classes扩展我自己的自定义小部件时防止重复的自动连接
- 使用 C++ 模板定义函数,但显式删除它以防止滥用
- 使用外部定义并防止重复定义
- 在接受用户定义的文字的排列时,防止过载爆炸
- 防止在宏中定义的匿名变量的解构,直到范围结束
- 我如何防止静态类成员变量需要两个定义/声明
- 定义析构函数可以防止成员函数内联
- 从 STL 容器继承并删除"新"运算符以防止由于缺少虚拟析构函数而导致未定义的行为是否有意义?
- 什么是C++中的不确定行为?它与未定义的行为有何不同
- 防止重新定义构造函数定义的方法
- 在类的私有部分为复制构造函数定义原型如何防止类的复制
- 如何在这个无锁堆栈函数中防止未定义的行为和ABA问题
- 如何防止.h文件中函数的多个定义
- 防止包括从hxx(模板定义)文件结束头
- 如何保护数组定义,防止使用非零值进行不完整的初始化
- 如何防止自定义QWidget被调整大小
- 在链接两个对象文件时,使用#ifndef防止定义一个函数两次
- 防止自定义迭代器获取nullptr
- 防止或生成自定义弃用的警告