标题互相包含在C 中

headerd include each other in c++

本文关键字:包含 标题      更新时间:2023-10-16

我试图这样做时有错误:

在main.cpp文件中 -

#include <iostream>
#include "Image.h"
using namespace std;
using namespace imaging;
int main()
{
    char onoma_arxeiou[100];
    cout<<"Dwse onoma arxeiou: ";
    cin>>onoma_arxeiou;
    Image im(5,6);
    im<<onoma_arxeiou;
}

image.h

 #include "ppm_format.h"
using namespace std;
namespace imaging
{
    class Image
    {
        bool Image::operator << (std::string filename)
        {
           ReadPPM(filename.c_str());
        }
    };

和ppm.h

class Image;
//#include "Image.h"
namespace imaging
{
    void ReadPPM(const char * filename);
    bool WritePPM(Image & image, const char * filename);
} 

但是在main中看不到Image有这些错误:

错误:引用"图像"是模棱两可的

Image im(5,6);

请有任何帮助吗?

ppm.h失败的原因,您的图像有向前声明:

class Image;

这是在全局名称空间中,而不是在您的imaging名称空间中。

要解决此问题,将您的正向声明放在imaging名称空间内。

namespace imaging
{
  class Image;
  void ReadPPM(const char * filename);
  /// ...
}