【发布时间】:2014-04-22 11:20:34
【问题描述】:
我可以在 1 个头文件中声明我的类并在 2 个单独的 cpp 文件中定义它吗? (就像在 C# 中一样)
主要原因是我减少了我现在拥有的单个文件中类定义的行数。顺便说一句,我所有的标题都是“包括受保护的”+“pragma onced”。
标头: "foo.h"
#pragma once
#ifndef FOO_H_2014_04_15_0941
#define FOO_H_2014_04_15_0941
class CFoo
{
public:
int add(int a, int b);
int sub(int a, int b);
};
#endif
来源: "foo.cpp"
#include "stadafx.h"
#include "foo.h"
int CFoo::add(int a, int b)
{
return a + b;
}
和"foo2.cpp"
#include "stadafx.h"
#include "foo.h"
int CFoo::sub(int a, int b)
{
return a - b;
}
当我尝试时,在第二个 cpp 文件“无法打开源文件 stdafx.h”(也是“foo.h”)中出现编译器错误
【问题讨论】:
-
为了所有神圣的爱,请不要这样做。大多数 C++ 开发人员不会预料到这一点,并且它成为维护的噩梦。如果你的类太大需要 2 个文件,那么你需要修复你的设计,可能你需要更多的对象。
-
@paulm 实际上这不是我的设计,现在是我维护它。我想将含义不同的方法分开,也通过
private / public分开。
标签: c++ oop visual-c++