首页 > 解决方案 > 无法在头文件 C++ 中声明类

问题描述

我的代码有点麻烦。我想在头文件中声明类并将其调用到我的 CPP 文件中。但我总是得到错误,'coba' 不是类或命名空间名称。

这是我的头文件,'testFile.hpp':

#pragma once

class coba
{
private:
    int num;
public:
    //coba();
    int getNum();
};

这是我的 CPP 文件,“testFile.cpp”:

#include "testFile.hpp"
#include "pch.h"

int coba::getNum()
{
    return 10;
}

标签: c++visual-studio

解决方案


这有效:

头文件,'testFile.hpp':

#ifndef __coba__
#define __coba__

class Coba
{
private:
  int num;
public:
  Coba();
  int getNum();
};

#endif

这是我的 CPP 文件,“testFile.cpp”:

#include "testFile.hpp"

Coba::Coba()
{
}

int Coba::getNum()
{
  return 10;
}

推荐阅读