【发布时间】:2012-02-12 18:11:01
【问题描述】:
我正在开发一个 C++ 项目。
我有一个类及其函数,然后我意识到其中一些函数与该类无关,而只是数学函数,因此我决定将它们移至命名空间。
我的第一个问题是,c++ 命名空间的适当文件扩展名是什么?
我有一个 constants.h 文件,我计划在其中保存全局常量,例如 PI。
现在:
#include <math.h>
const double PI = M_PI;
我有之前讲过的命名空间,现在叫做:specificMath.h
#include <stdlib.h>
#include "constants.h"
... more code
我有一个 gaussian.cpp:
#include "gaussian.h"
#include "specificMath.h"
#include <iostream>
... more code
这个文件包含一个现在什么都不做的 main 函数,我只是无法让整个项目在没有 main 的情况下编译...
我有一个 gaussian.h,其中没有包含任何内容,这有问题吗?
第三个类,它没有属性,只有方法(同样,这是错的吗?还是不漂亮?)。截断函数.cpp
#include "specificMath.h"
#include <stdlib.h>
#include "truncatedFunctions.h"
#include "gaussian.h"
using namespace specificMath;
还有它的 truncatedFunctions.h,我又一次没有包括任何东西。
还有我包括的第四节课
#include "margin.h" //Its header, where I'm not including anything
#include "gaussian.h"
#include "specificMath.h"
using namespace specificMath;
当我“制作”它时,它似乎编译得很好,但是当它到达链接部分时,我得到了很多错误,说我的 margin.cpp 类上的东西首先在 truncatedFunctions.cpp 中定义
我快疯了。我不知道为什么会这样,也不知道如何解决。如果有人可以帮助我,我将非常感激,并且请,任何额外的建议都会很棒,因为我真的想尽可能多地学习这个项目。谢谢!
【问题讨论】:
标签: c++ class namespaces