[英]C++ valarray/template classes not working
this is my first post here but I've been a frequent reader of various topics here.这是我在这里的第一篇文章,但我一直是这里各种主题的常客。
Now I'm stuck with a programming issue with c++, its basically a template class called "Pair" which should contain 2 valarrays of ints and then be included in another class called Wine.现在我遇到了 c++ 的编程问题,它基本上是一个名为“Pair”的模板 class,它应该包含 2 个整数值,然后包含在另一个名为 Wine 的 class 中。 Problem is I'm not getting either the constructors right or the header file according to my compiler!问题是根据我的编译器,我没有得到正确的构造函数或 header 文件!
Take a look and please try to help me, the main issue is that it the valarrays wont take ints as arguments + i dont understand how i can convert a usual int array to a valarray with just 1 constructor argument:看一下,请尝试帮助我,主要问题是 valarrays 不会将 int 视为 arguments + 我不明白如何仅使用 1 个构造函数参数将通常的 int 数组转换为 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
So, heres the header file, now comes the first.cpp file with all the function - definitions:所以,这里是 header 文件,现在是第一个包含所有 function 的.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++;
};
}
Finally the last.cpp file:最后是 last.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;
}
I would be enourmosly grateful if you guys could tell me whats wrong and how to fix it.如果你们能告诉我出了什么问题以及如何解决它,我将不胜感激。 Im currently doing stephen pratas C++ book and this is a exercise, thanks!我目前正在做 stephen pratas C++ 的书,这是一个练习,谢谢!
Any other general tips on coding would be wonderful aswell, have a good time!任何其他关于编码的一般提示也会很棒,玩得开心!
What's wrong: Well, honestly, where do I start?怎么了:老实说,我从哪里开始?
Firstly, there is a std::pair
structure.首先,有一个std::pair
结构。 Secondly, the valarray
stuff was a total mistake and not at all used anymore.其次, valarray
的东西完全是一个错误,根本不再使用。 Thirdly, const char*
, int[]
arguments?第三, const char*
, int[]
arguments? Owch.哎呀。 Can you say buffer overrun and memory corruption?你能说缓冲区溢出和 memory 损坏吗? Fourthly,第四,
int Wine::sum()
{
int b;
int ar = 0;
while(arr.second()[b])
{
ar += arr.second()[b];
b++;
}
return ar;
}
You didn't initialize b.你没有初始化 b。 Undefined behaviour.未定义的行为。
The Definitive C++ Book Guide and List 权威的 C++ 书籍指南和列表
This question lists good C++ books, and Stephen Prata is mentioned as having a very bad book.这个问题列出了好的 C++ 书籍,并且提到斯蒂芬普拉塔有一本非常糟糕的书。 This code sample supports that.此代码示例支持这一点。 Burn your book and buy one that doesn't suck, would be my recommendation.烧掉你的书,买一本不烂的书,这是我的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.