我是C / C ++的新手,并试图提高编码技巧。我正在写一个最小的代码来计算我大学学习中使用的公式。此代码由许多类组成。虽然我正在尝试构建它,但我在课堂上的标题中得到了错误。这是我的代码:
materials.hpp
#ifndef MATERIALS_HPP
#define MATERIALS_HPP
#include <cmath>
#include <iostream>
using namespace std;
class materials{
public:
double MassNumber, AtomNumber, Density, I, C, a, m, X1, X0, X, delta;
materials();
materials(double, double, double, double, double, double, double, double, double);
~materials();
};
materials Magnesium;
materials Cupper;
materials Alluminium;
materials Plumbo;
materials Water;
#endif // MATERIALS_HPP
这是来源;
materials.cpp
#include "materials.hpp"
#include <cmath>
#include <iostream>
using namespace std;
materials::materials()
{
MassNumber = AtomNumber = Density = I = a = m = C = X1 = X0 = X = delta = 0.0;
}
materials::materials(double a1, double a2, double a3, double a4, double a5, double a6,double a7, double a8, double a9)
{
MassNumber = a1;
AtomNumber = a2;
Density = a3;
I = a4;
C = a5;
a = a6;
m = a7;
X1 = a8;
X0 = a9;
}
Magnesium.materials(23.0, 12.0, 1.738, 156.0, -4.53, 0.0816, 3.62, 3.07, 0.1499);
Cupper.materials(63.0, 29.0, 8.96, 322.0, -4.42, 0.1434, 2.90, 3.28, 0.0254);
Alluminium.materials(26.0, 13.0, 2.70, 166.0, -4.24, 0.0802, 3.63, 3.01, 0.1708);
Plumbo.materials(207.0, 82.0, 11.34, 823.0, -6.20, 0.0936, 3.16, 3.81, 0.3776);
Water.materials(18.0, 10.0, 1.0, 75.0, -3.50, 0.0911, 3.48, 2.80, 0.2400);
我得到的错误;
materials.cpp:64:1: error: 'Magnesium' does not name a type
Magnesium.materials(23.0, 12.0, 1.738, 156.0, -4.53, 0.0816, 3.62, 3.07, 0.1499
);
^
materials.cpp:66:1: error: 'Cupper' does not name a type
Cupper.materials(63.0, 29.0, 8.96, 322.0, -4.42, 0.1434, 2.90, 3.28, 0.0254);
^
materials.cpp:68:1: error: 'Alluminium' does not name a type
Alluminium.materials(26.0, 13.0, 2.70, 166.0, -4.24, 0.0802, 3.63, 3.01, 0.1708
);
^
materials.cpp:70:1: error: 'Plumbo' does not name a type
Plumbo.materials(207.0, 82.0, 11.34, 823.0, -6.20, 0.0936, 3.16, 3.81, 0.3776);
^
materials.cpp:72:1: error: 'Water' does not name a type
Water.materials(18.0, 10.0, 1.0, 75.0, -3.50, 0.0911, 3.48, 2.80, 0.2400);
^
make: *** [materials.o] Error 1
我在Linux上使用GCC 4.9.0,在Windows上使用GCC 4.8.1在MinGW上尝试。我读过有关它的其他文章,但我无法做到。为什么我会收到这个错误?
答案 0 :(得分:2)
你应该去了解C ++中的类如何工作的一个很好的介绍。
问题是,你试图做一些奇怪的两部分构造,首先声明
materials Magnesium;
然后尝试显式调用
Magnesium.materials(23.0, 12.0, 1.738, 156.0, -4.53, 0.0816, 3.62, 3.07, 0.1499);
但是构造函数在C ++中不像那样工作!
你应该这样做:
materials Magnesium(23.0, 12.0, 1.738, 156.0, -4.53, 0.0816, 3.62, 3.07, 0.1499);
这不是解释课堂建设细节的地方,但我可以肯定地说你应该对此进行更多的研究。
答案 1 :(得分:1)
如果您希望在其他源文件中访问这些全局变量:
在标题文件中更改为
extern materials Magnesium;
extern materials Cupper;
extern materials Alluminium;
extern materials Plumbo;
extern materials Water;
在源文件中更改为
materials Magnesium(23.0, 12.0, 1.738, 156.0, -4.53, 0.0816, 3.62, 3.07, 0.1499);
materials Cupper(63.0, 29.0, 8.96, 322.0, -4.42, 0.1434, 2.90, 3.28, 0.0254);
materials Alluminium(26.0, 13.0, 2.70, 166.0, -4.24, 0.0802, 3.63, 3.01, 0.1708);
materials Plumbo(207.0, 82.0, 11.34, 823.0, -6.20, 0.0936, 3.16, 3.81, 0.3776);
materials Water(18.0, 10.0, 1.0, 75.0, -3.50, 0.0911, 3.48, 2.80, 0.2400);
如果您不希望在其他源文件中访问这些全局变量:
从头文件中删除它:
materials Magnesium;
materials Cupper;
materials Alluminium;
materials Plumbo;
materials Water;
在源文件中更改为
static materials Magnesium(23.0, 12.0, 1.738, 156.0, -4.53, 0.0816, 3.62, 3.07, 0.1499);
static materials Cupper(63.0, 29.0, 8.96, 322.0, -4.42, 0.1434, 2.90, 3.28, 0.0254);
static materials Alluminium(26.0, 13.0, 2.70, 166.0, -4.24, 0.0802, 3.63, 3.01, 0.1708);
static materials Plumbo(207.0, 82.0, 11.34, 823.0, -6.20, 0.0936, 3.16, 3.81, 0.3776);
static materials Water(18.0, 10.0, 1.0, 75.0, -3.50, 0.0911, 3.48, 2.80, 0.2400);