如何防止重新定义类?

How to prevent Redefinition of class?

本文关键字:定义 何防止 新定义      更新时间:2023-10-16

我一直在从最新的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. .常见的做法是遵循下面的项目结构(使用防护指令(:

类名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;
};