开发者

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. Problem is I'm not getting either the constructors right or the header file according to my compiler!

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:

#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:

#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;
i开发者_JS百科nt 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:

#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!

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. Secondly, the valarray stuff was a total mistake and not at all used anymore. Thirdly, const char*, int[] arguments? Owch. Can you say buffer overrun and memory corruption? 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. Undefined behaviour.

The Definitive C++ Book Guide and List

This question lists good C++ books, and Stephen Prata is mentioned as having a very bad book. This code sample supports that. Burn your book and buy one that doesn't suck, would be my recommendation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜