我发现在一个单独的Cpp文件中定义了一个结构(带有一个双精度数组和一个整数数组),但是从main调用的结果将不合理的值发送给cout用于数组.下面我希望是最小的例子,以及控制台输出.
我的道歉应该是我的代码被扰乱 - 我一直在努力格式化它.
如果有人能帮助我理解并纠正这一点,我将不胜感激.
最好,乔
(1)main.cpp:
#include "iostream"
#include "defs.h"
using namespace std;
int main()
{
MyStruct myModel=ConstructModel();
cout << endl << "myModel goes first:" << endl;
for(int i=0; i<myModel.n; i++)
cout << "myModel.Y[" << i << "]=" << myModel.Y[i] << endl;
cout << "myModel.n=" << myModel.n << endl;
MyStruct myOtherModel;
myOtherModel.n=2; double Y[2]={0.1,0.1};
myOtherModel.Y=Y;
cout << endl << "now myOtherModel:" << endl;
for(int i=0; i<myModel.n; i++)
cout << "myOtherModel.Y[" << i << "]=" << myOtherModel.Y[i] << endl;
return …Run Code Online (Sandbox Code Playgroud) 我试图递归地使用模板来定义(在编译时)双元组的d元组.下面的代码可以很好地编译Visual Studio 2010,但是g ++失败并且抱怨它"无法直接调用构造函数'指向<1> :: point'".
有谁能请说明这里发生的事情?
非常感谢,乔
#include <iostream>
#include <utility>
using namespace std;
template <const int N>
class point
{
private:
pair<double, point<N-1> > coordPointPair;
public:
point()
{
coordPointPair.first = 0;
coordPointPair.second.point<N-1>::point();
}
};
template<>
class point<1>
{
private:
double coord;
public:
point()
{
coord= 0;
}
};
int main()
{
point<5> myPoint;
return 0;
}
Run Code Online (Sandbox Code Playgroud)