Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6162051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:35:14+00:00 2026-05-23T21:35:14+00:00

this is my first post here but I’ve been a frequent reader of various

  • 0

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:35:15+00:00Added an answer on May 23, 2026 at 9:35 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usless Background Info Hello, all. This is my first post here, but I often
This is my first post here and I wanted to get some input from
Well, this is my first post here and really enjoying the site. I have
This is my first post here - so be gentle :-) I want to
Sorry folks...first post here. Just getting the hang of this site. Thanks for the
This is my first post and I'm quite a novice on C++ and compiling
problem euler #5 i found the solution but i don't know why this first
this is my first question to stackoverflow so here it goes... I use cruise
this is my first question here so I hope I can articulate it well
A good day dear developers. My name is Danny. This is my first post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.