这是我在这里的第一篇文章,但我经常阅读这里的各种主题。
现在我被 c++ 的编程问题困住了,它基本上是一个名为“Pair”的模板类,它应该包含 2 个 valarrays 的整数,然后包含在另一个名为 Wine 的类中。问题是根据我的编译器,我没有得到正确的构造函数或头文件!
看一看,请尝试帮助我,主要问题是 valarrays 不会将 int 作为参数 + 我不明白如何将一个普通的 int 数组转换为只有 1 个构造函数参数的 valarray:
#ifndef Derp
#define Derp
#include <valarray>
template <typename T1, typename T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const {return a;}
T1 second() const {return b;}
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) {}
Pair() {}
};
template Pair<std::valarray<int>, std::valarray<int> >;
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
std::string name;
int years;
PairArray arr;
public:
Wine(const char * l, int y, const int yr[], const int bot[]);
Wine(const char * l, int y);
void GetBottles();
std::string Label();
int sum();
void show();
};
#endif
所以,这是头文件,现在是第一个包含所有函数定义的 .cpp 文件:
#include <iostream>
#include <valarray>
#include <cstring>
#include "K14O1.h"
template <typename T1, typename T2>
T1 & Pair<T1, T2>::first()
{
return a;
}
template <typename T1, typename T2>
T2 & Pair<T1, T2>::second()
{
return b;
}
Wine::Wine(const char * l, int y, const int yr[], const int bot[])
: arr(y, y)
{
name = l;
years = y;
for(int a = 0; a < y; a++)
{
arr.first()[a] = yr[a];
arr.second()[a] = bot[a];
}
}
Wine::Wine(const char * l, int y)
: arr()
{
name = l;
years = y;
arr.first() = y;
arr.second() = y;
}
void Wine::GetBottles()
{
for(int c = 0; c < years; c++)
{
std::cout << "Skriv in antal buteljer för det året: ";
std::cin >> arr.first()[c];
std::cout << "Skriv in årgång: ";
std::cin >> arr.second()[c];
}
}
std::string Wine::Label()
{
return name;
}
typedef std::valarray<int> ArrayInt;
int Wine::sum()
{
int b;
int ar = 0;
while(arr.second()[b])
{
ar += arr.second()[b];
b++;
};
return ar;
}
void Wine::show()
{
std::cout << "Vin: " << name << std::endl;
int b = 0;
while(arr.first()[b])
{
std::cout << arr.first()[b] << "\t" << arr.second()[b] << std::endl;
b++;
};
}
最后一个.cpp文件:
#include <iostream>
#include <valarray>
#include "K14O1.h"
using namespace std;
int main(int argc, char * argv[])
{
const int YRS = 3;
int y[YRS] = {1993, 1995, 1998};
int b[YRS] = {48, 60, 72};
Wine more("Gushing Grape Red", YRS, y, b);
cout << "Skriv in vinets namn: ";
char lab[50];
cin.getline(lab, 50);
cout << "Skriv in antal årgångar: ";
int yrs;
cin >> yrs;
Wine holding(lab, yrs);
holding.GetBottles();
holding.show();
more.show();
cout << "Totalt antal buteljer av " << more.Label()
<< ": " << more.sum() << endl;
cout << "HEJDASADAN" << endl;
return 0;
}
如果你们能告诉我哪里出了问题以及如何解决,我将不胜感激。我目前正在写 stephen pratas C++ 书,这是一个练习,谢谢!
任何其他关于编码的一般技巧也将很棒,玩得开心!
最佳答案
怎么了:老实说,我该从哪里开始呢?
首先,有一个std::pair
结构。其次,valarray
完全是个错误,根本不再使用了。第三,const char*
, int[]
参数?哎哟。你能说缓冲区溢出和内存损坏吗?第四,
int Wine::sum()
{
int b;
int ar = 0;
while(arr.second()[b])
{
ar += arr.second()[b];
b++;
}
return ar;
}
你没有初始化 b。未定义的行为。
The Definitive C++ Book Guide and List
这个问题列出了优秀的 C++ 书籍,并提到 Stephen Prata 有一本非常糟糕的书。此代码示例支持这一点。烧掉你的书,买一本不烂的书,这是我的建议。
关于C++ valarray/模板类不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6724671/