C++ valarray/template classes not working - c++

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!

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.

Related

Using a function pointer array to call a method of a class

I would like to write a program in C++ which contains an array of function pointers.
Here is the code:
#include <iostream>
using namespace std;
class MyClass {
int a, b;
public:
MyClass(int i, int j) : a(i), b(j) {}
int add() { return a + b; }
int sub() { return a - b; }
};
void func(int (MyClass::* funcPtr[])(), MyClass& a, int i) {
if (i == 0) {
funcPtr[i] = &MyClass::add;
funcPtr;
}
if (i == 1) {
funcPtr[i] = &MyClass::sub;
funcPtr;
}
cout << " Result: " << (a.*funcPtr[i])() << endl;
}
int main(){
int auswahl = 0;
int i = 4, j = 5;
cout << "Which function? [0]-Add [1]-Substract\n";
cin >> select;
MyClass a(i,j);
func(NULL, a, select);
}
After playing around a lot I got the program to compile successfully. But it throws "Write Access Violation" on running.
The problem seems to be related to:
funcPtr[i] = &MyClass::add;
funcPtr[i] = &MyClass::sub;
It'd be very nice, if you could help me solve the problem.
Thank you so much and have a happy time!
As you pass NULL or nullptr in your function, so this line:
funcPtr[i] = &MyClass::add;
Is writing at index i into a null array!
You'd have to provide an array for your function to write into:
MyClass a(i,j);
int (MyClass::* funcPtr[2])();
func(funcPtr, a, select);
Note that using std::array instead of c-style array would avoid this problem, since they are not nullable:
void func(std::array<int (MyClass::*)(), 2> funcPtr, MyClass& a, int i) {
// ...
}
// ...
std::array<int (MyClass::*)(), 2> funcPtr;
func(funcPtr /* cannot pass null */, a, i);

nullptr can't use in valarray

Why can't use nullptr in the constructor function?( the function name: Wine) When i try to do this, the program will break down and no any error report maybe because i don't the reason for that.
#ifndef WINE_H_
#define WINE_H_
#include<iostream>
#include<string>
#include<valarray>
using std::string;
using std::valarray;
template<typename T1, typename T2>
class Pair //member of the wine
{
private:
T1 a;
T2 b;
public:
T1 & first(){ return a; }
T2 & second(){ return b; }
T1 first()const{ return a; }
T2 second()const{ return b; }
Pair(const T1 & aval, const T2 & bval) :a(aval), b(bval){}
Pair(){}
};
typedef valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt>PairArray;
class Wine
{
private:
string name;
PairArray bt;
int years;
public:
Wine();
Wine(const char * a, int y,int b[], int c[]); //no problem
Wine(const char * a, int y); //here is that problem function
void GetBottles(); //no problem
void Show()const; //no problem
int Sum(){ return bt.second().sum(); }
};
Wine::Wine(const char * a, int y) :name(a), years(y), bt(ArrayInt(0, y), ArrayInt(0, y)){}
**//When I am trying to use nullptr to instead 0 in the ArrayInt(0,y),the whole program will break down during work.**
Wine::Wine(const char * a, int y, int b[], int c[]) :bt(ArrayInt(b, y), ArrayInt(c, y))
{
name = a;
years = y;
}
Wine::Wine() :bt(ArrayInt(),ArrayInt())
{
name = "null";
years = 0;
}
void Wine::GetBottles()
{
std::cout << "Please input the years and the bottles\n";
for (int i = 0; i < years; i++)
{
std::cout << "input the year: ";
(std::cin >> bt.first()[i]).get();
std::cout << "input the bottles";
(std::cin >> bt.second()[i]).get();
}
}
void Wine::Show()const
{
using std::cout;
using std::endl;
for (int i = 0; i < years; i++)
{
cout << bt.first()[i] << '\0' << bt.second()[i] << endl;
}
}
#endif
#include<iostream> //test part
#include"wine.h"
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
cout << "Enter name of wine: ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter number of years: ";
int yrs;
cin >> yrs;
Wine holding(lab, yrs);
holding.GetBottles();
holding.Show();
return 0;
}
Thank your for your help!
This is a funny one. The reason why it breaks in one example, but not another is following:
There are two different constructors for std::valarray (more than that, but those two matter):
valarray( const T& val, std::size_t count ); // 1
valarray( const T* vals, std::size_t count ); // 2
When you use 0 (valarray(0, y)) you are calling the first version - creating an array of y elements, where every element is initialized to 0.
But when you are calling it with nullptr, you are calling the second version of it - trying to initialize your new array with a copy from an array pointed to by the first argument to the constructor. But your first argument is nullptr, and any attempt to use at as an array triggers undefined behavior, and program crashes.

Function converting variable Type to Type* creating error C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I keep getting an error of:
In file included from user.h:3:0,
from sn.cpp:5:
'mylist.h: In member function ‘void MyList<L>::push_back(L) [with L = int]’:
user.h:38:30: instantiated from here
mylist.h:54:3: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
mylist.h: In member function ‘void MyList<L>::push_back(L) [with L = User*]’:
sn.cpp:61:25: instantiated from here
mylist.h:54:3: error: cannot convert ‘User*’ to ‘User**’ in assignment
make: *** [sn.o] Error 1
I am creating a rudimentary social network where main takes 3 command line arguments - argv[1] is a GML file with nodes containing user information and edges that are user connections. argv[2] is another file that I have not yet processed. and argv[3] is a GML file that will contain basically a copy of the user information after it has been parsed and put into an ADT list MyList that I wrote, containing instances of User* that hold private data for user id, name, zip code, and age. For some reason my pushback function to add another item to my list is either making pointers double pointers or nonpointers pointers which creates the error above. I just cannot figure out where I need to remove a * or what I did wrong. the GML reader function populates two vectors nodes and edges with information such as
nodes[0] = id 0 name "Mark Redekopp" age 34 zip 90018
nodes[1] = id 1 name "Tommy Trojan" age 124 zip 90007
and
edges[0] = source 0 target 1
edges[1] = source 1 target 0
The code to write a new GML file is not included yet
sn file
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "user.h"
#include "mylist.h"
#include "gmlreader.h"
using namespace std;
int main(int argc, char* argv[]){
if(argc < 4){
cerr << "Please provide the input GML file, command file, and output file" << endl;
return 1;
}
vector<string>nodes;
vector<string>edges;
GMLReader::read(argv[1], nodes, edges);
for(unsigned int i =0; i<nodes.size(); i++){
cout << "node[" << i << "]: " << nodes[i] << endl;
};
for(unsigned int i=0; i<edges.size(); i++){
cout << "edge[" << i << "]: " << edges[i] << endl;
cout << "printing an edge!" << endl;
};
cout << "about to create a mylist of users" << endl;
MyList<User*>Users;
cout << "initialized user list" << endl;
for(unsigned int i =0; i<nodes.size(); i++){
string TextBlob = nodes[i];
stringstream ss(TextBlob);
cout << "started string stream" << endl;
User* newuser = new User;
while(newuser->getName()=="" || newuser->getId()==0 || newuser->getZip()==0|| newuser->getAge()==0){
if (TextBlob.compare("name")==0){
string n;
ss>>n;
newuser->setName(n);
}
else if(TextBlob.compare("age")==0){
int a;
ss>>a;
newuser->setAge(a);
}
else if(TextBlob.compare("id")==0){
int d;
ss>>d;
newuser->setId(d);
}
else if(TextBlob.compare("zip")==0){
int z;
ss>>z;
newuser->setZip(z);
}
}
Users.push_back(newuser);
}
return 0;
};
mylist.h
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#ifndef MYLIST_H
#define MYLIST_H
using namespace std;
template<typename L>
class MyList{
private:
L* data_;
int len_;
int MAX_LIST_SIZE;
public:
MyList();
~MyList();
void push_back(L newVal);
int size();
L& at(int loc);
bool remove(L val);
L pop(int loc);
L& operator[](int loc);
void changeLen(int new_len){
len_=new_len;
}
};
int MAX_LIST_SIZE=100;
template<typename L>
MyList<L>::MyList(){
data_ = new L[MAX_LIST_SIZE];
len_=0;
};
template<typename L>
MyList<L>::~MyList(){
delete [] data_;
};
template<typename L>
void MyList<L>::push_back(L newVal){
if(len_==MAX_LIST_SIZE-1){
L* tempList = new L[MAX_LIST_SIZE*2];
for(int i=0; i<len_; i++){
tempList[i]=data_[i];
MAX_LIST_SIZE*=2;
}
tempList[len_++]=newVal;
data_=newVal;
}
data_[len_++]=newVal;
};
template<typename L>
int MyList<L>::size(){
return len_;
};
template<typename L>
L& MyList<L>::at(int loc){
if(loc > len_)
throw invalid_argument("Out of bounds");
return data_[loc];
};
template<typename L>
bool MyList<L>::remove(L val){
for(int i=0; i<len_; i++){
if(data_[i]==val){
for(int j=i; j<len_-1; j++){
data_[j]=data_[j+1];
}
changeLen(len_-1);
return true;
};
};
return false;
};
template<typename L>
L MyList<L>::pop(int loc){
if(loc>len_)
throw invalid_argument("Out of bounds");
L temp;
data_[loc] = temp;
for(int i=len_; i>=loc; i--){
data_[i-1]=data_[i];
};
changeLen(len_-1);
return temp;
};
template<typename L>
L& MyList<L>::operator[](int loc){
return data_[loc];
};
#endif
user.h
#ifndef USER_H
#define USER_H
#include "mylist.h"
class User{
public:
User(){
name_=""; age_ =0; zip_=0; id_=0;};
~User();
void setName(string name){
name_=name;
};
string getName(){
return name_;
};
void setAge(int age){
age_=age;
};
int getAge(){
return age_;
};
void setId(int id){
id_=id;
};
int getId(){
return id_;
};
void setZip(int zip){
zip_=zip;
};
int getZip(){
return zip_;
};
MyList<int> getFriends(){
return Friends;
};
void addFriend(int friendid){
Friends.push_back(friendid);
};
void printUser(){
cout<< "User Name: " << name_ << endl;
cout<< "User Age: " << age_ << endl;
cout<< "User's Friends: ";
for(int j=0; j<Friends.size(); j++){
cout <<Friends.at(j) << " ";
};
cout << endl;
};
private:
string name_;
int age_;
int id_;
int zip_;
MyList<int> Friends;
};
#endif
The issue is in push_back, specifically, this line:
data_=newVal;
newVal is an L, but data_ is an L*. I think what you meant to say is data_ = tempList.
Don't forget to delete the old value for data_, though.

Priority queue for user-defined types

I have the below struct:
struct node {
float val;
int count;
}
I have several objects of this struct. Now, I want to insert these objects into a priority queue of STL such that the priority queue orders the items by count. Any idea on how to do so? Preferably a min-heap is preferred. I know how to do the above for primitive data types, not structs
Overload the < operator:
bool operator<(const node& a, const node& b) {
return a.count > b.count;
}
I have reversed the comparison to achieve min heap without passing extra arguments to the priority queue.
Now you use it like this:
priority_queue<node> pq;
...
Edit: take a look at this post which seems to be almost exact duplicate: STL Priority Queue on custom class
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Boxer{
public:
string name;
int strength;
};
struct Comp{
bool operator()(const Boxer& a, const Boxer& b){
return a.strength<b.strength;
}
};
int main(){
Boxer boxer[3];
boxer[0].name="uday", boxer[0].strength=23;
boxer[1].name="manoj", boxer[1].strength=33;
boxer[2].name="rajiv", boxer[2].strength=53;
priority_queue< Boxer, vector<Boxer>, Comp> pq;
pq.push(boxer[0]);
pq.push(boxer[1]);
pq.push(boxer[2]);
Boxer b = pq.top();
cout<<b.name;
//result is Rajiv
return 0;
}
Using greater as comparison function you can use priority queue as min heap,
#include <bits/stdc++.h>
using namespace std;
int main()
{
priority_queue<int,vector<int>,greater<int> >pq;
pq.push(1);
pq.push(2);
pq.push(3);
while(!pq.empty())
{
int r = pq.top();
pq.pop();
cout << r << " ";
}
return 0;
}
Inserting value by changing their sign (using minus (-) for positive number and using plus (+) for negative number we can use priority queue in reversed order.
int main()
{
priority_queue<int>pq2;
pq2.push(-1); //for +1
pq2.push(-2); //for +2
pq2.push(-3); //for +3
pq2.push(4); //for -4
while(!pq2.empty())
{
int r = pq2.top();
pq2.pop();
cout << -r << " ";
}
return 0;
}
For custom data types or classes we need a to tell priority queue a way of knowing on which order it will sort our data.
struct compare
{
bool operator()(const int & a, const int & b)
{
return a>b;
}
};
int main()
{
priority_queue<int,vector<int>,compare> pq;
pq.push(1);
pq.push(2);
pq.push(3);
while(!pq.empty())
{
int r = pq.top();
pq.pop();
cout << r << " ";
}
return 0;
}
For custom structure or class you can use priority_queue in any order. Suppose, we want to sort people in descending order according to their salary and if tie then according to their age.
struct people
{
int age,salary;
};
struct compare {
bool operator()(const people & a, const people & b)
{
if(a.salary==b.salary)
{
return a.age>b.age;
} else {
return a.salary>b.salary;
}
}
};
int main()
{
priority_queue<people,vector<people>,compare> pq;
people person1,person2,person3;
person1.salary=100;
person1.age = 50;
person2.salary=80;
person2.age = 40;
person3.salary = 100;
person3.age=40;
pq.push(person1);
pq.push(person2);
pq.push(person3);
while(!pq.empty())
{
people r = pq.top();
pq.pop();
cout << r.salary << " " << r.age << endl;
}
Same result can be obtained by operator overloading :
struct people
{
int age,salary;
bool operator< (const people & p) const
{
if(salary==p.salary)
{
return age>p.age;
} else {
return salary>p.salary;
}
}
};
In main function :
priority_queue<people> pq;
people person1,person2,person3;
person1.salary=100;
person1.age = 50;
person2.salary=80;
person2.age = 40;
person3.salary = 100;
person3.age=40;
pq.push(person1);
pq.push(person2);
pq.push(person3);
while(!pq.empty())
{
people r = pq.top();
pq.pop();
cout << r.salary << " " << r.age << endl;
}
You need to provide operator< for that struct. Something like:
bool operator<(node const& x, node const& y) {
return x.count < y.count;
}
Now you can use a priority queue from the standard library.
Since C++11, you can write
auto comparer = [](const auto& a, const auto& b) {
return a.priority < b.priority;
};
std::priority_queue<Item, std::vector<Item>, decltype(comparer)> queue(comparer);
We can define user defined comparator class:
Code Snippet :
#include<bits/stdc++.h>
using namespace std;
struct man
{
string name;
int priority;
};
class comparator
{
public:
bool operator()(const man& a, const man& b)
{
return a.priority<b.priority;
}
};
int main()
{
man arr[5];
priority_queue<man, vector<man>, comparator> pq;
for(int i=0; i<3; i++)
{
cin>>arr[i].name>>arr[i].priority;
pq.push(arr[i]);
}
while (!pq.empty())
{
cout<<pq.top().name<<" "<<pq.top().priority;
pq.pop();
cout<<endl;
}
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Person
{
public:
string name;
int age;
Person(string str,int num)
{
name = str;
age = num;
}
};
// FUNCTOR
class compare
{
public:
bool operator()(Person a,Person b)
{
cout << "Comparing " << a.age << " with " << b.age << endl;
return a.age < b.age;
}
};
int main()
{
int n;
cin >> n;
priority_queue <Person, vector<Person> , compare> pq;
for(int i=1;i<=n;i++)
{
string name;
int x;
cin >> name;
cin >> x;
Person p(name,x);
pq.push(p);
}
int k = 3;
for(int i=0;i<k;i++)
{
Person p = pq.top();
pq.pop();
cout << p.name << " " << p.age << endl;
}
return 0;
}
Operator() is also commonly overloaded to implement functors or function object. For example we have a structure Person which have some default ways of searching and sorting a person by age but we want our customized ways with some other parameter like weight so we may use our own custom functor. Priority queue is one such container which accepts a functor so it knows how to sort the objects of custom data types. Each time a comparison has to be done, a object is instantiated of class compare, and it is passed two objects of person class for comparison.

Templates C++ error : could not deduce template argument

I am trying ot add a function template that will print if it contains precision values or valves and the value. The rest of the program works except this function. I am not sure what I am doing wrong but the error I recieve is:
error C2784: 'void printInstrumentDetail(const I *const )' : could not deduce template argument for 'const I *const ' from 'std::vector<_Ty>'
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
#include "Instruments.h"
#include "Brass.h"
#include "Strings.h"
using namespace std;
//template<typename I> <---Problem
//void printInstrumentDetail(const I * const a)
//{
// for (size_t i = 0; i < 6; i ++)
// {
// cout << "The details for " << a[i]->getName()
// << ": " << a[i]->print();
// }
//}
int main()
{
double total = 0;
Strings violin("Violin", 553.90, 3);
Strings cello("Cello", 876.45, 3);
Strings viola("Viola", 200.50, 23);
Brass tuba("Tuba", 1400.10, 1.23);
Brass trumpet("Trumpet", 500.00, 4.32);
Brass sax("Sax", 674.78, .99);
vector <Instruments *> band(6);
band[0] = &violin;
band[1] = &tuba;
band[2] = &cello;
band[3] = &trumpet;
band[4] = &viola;
band[5] = &sax;
cout << fixed << setprecision(2);
cout << "The instruments in the band are:\n";
//Get name and cost of each
for (size_t i = 0; i < 6; i ++)
{
cout << band[i]->getName() << " $"
<< band[i]->getCost() << endl;
}
cout << "\nThen band is warming up..." << endl;
//Get descrition of how sound is made of each
for (size_t i = 0; i < 6; i ++)
{
cout << "This " << band[i]->getName()
<< " makes sounds by " ;
band[i]->playSound();
}
cout << "\nTotal cost of the band is: $" ;
//Get total cost of all instruments
for (size_t i = 0; i < 6; i ++)
{
total = band[i]->getCost() + total;
}
cout << total << endl;
//printInstrumentDetail(band); <--Problem
return 0;
}
Here's the base class:
#ifndef INSTRUMENTS_H
#define INSTRUMENTS_H
#include <string>
using namespace std;
class Instruments
{
public:
Instruments(string, double);
void setName(string);
virtual string getName();
void setCost(double);
virtual double getCost();
virtual void print();
virtual void playSound();
private:
string name;
double cost;
};
#endif
#include <iostream>
#include "Instruments.h"
using namespace std;
Instruments::Instruments(string n, double c)
{
name = n;
cost = c;
}
void Instruments::setName(string n)
{
name = n;
}
string Instruments::getName()
{
return name;
}
void Instruments::setCost(double c)
{
cost = c;
}
double Instruments::getCost()
{
return cost;
}
void Instruments::print()
{
}
void Instruments::playSound()
{
//empty
}
Derived class Bass:
#ifndef BRASS_H
#define BRASS_H
#include <string>
#include "Instruments.h"
using namespace std;
class Brass : public Instruments
{
public:
Brass(string, double, double);
void setPrecisionValue(double);
double getPrecisionValue();
void print() ;
void playSound();
private:
double precision;
string sound;
};
#endif
#include <iostream>
#include "Brass.h"
using namespace std;
Brass::Brass(string n, double c, double p)
:Instruments(n, c)
{
precision = p;
}
void Brass::setPrecisionValue(double p)
{
precision = p;
}
double Brass::getPrecisionValue()
{
return precision;
}
void Brass::print()
{
cout << getPrecisionValue() << endl;
}
void Brass::playSound()
{
cout << "blowing in a mouthpiece." << endl;
Instruments::playSound();
}
Derived class Strings:
#ifndef STRINGS_H
#define STRINGS_H
#include <string>
#include "Instruments.h"
using namespace std;
class Strings : public Instruments
{
public:
Strings(string, double, int);
void setValves(int);
int getValves();
void print();
void playSound();
private:
int valves;
};
#endif
#include <iostream>
#include "Strings.h"
using namespace std;
Strings::Strings(string n, double c, int v)
:Instruments(n, c)
{
valves = v;
}
void Strings::setValves(int v)
{
valves = v;
}
int Strings::getValves()
{
return valves;
}
void Strings::print()
{
cout<< getValves() << endl;
}
void Strings::playSound()
{
cout << "striking with a bow." << endl;
Instruments::playSound();
}
Well, the problem is that your template requires a pointer:
template<typename I>
void printInstrumentDetail(const I * const a);
but you're giving it a vector, not a pointer:
vector <Instruments *> band(6);
...
printInstrumentDetail(band);
You can hack your way around this by passing a pointer to the printInstrumentDetail function, like so:
printInstrumentDetail(&band[0]);
But really, you'd be much better off modifying printInstrumentDetail to take a container or a pair of iterators:
template <typename ContainerT>
void printInstrumentDetail(const ContainerT& a)
or
template <typename IteratorT>
void printInstrumentDetail(IteratorT first, IteratorT last)
with the appropriate modifications to the definition of the function.
Pass the pointer to vector
printInstrumentDetail(&band);
and inside printInstrumentDetail
(*a)[i]->getName();
Well, first off I don't believe you can pass a vector as a const * I const at
printInstrumentDetail(band);
Vector cannot be just cast to a pointer. One working solution would be something like:
template <typename T>
void printInstrumentDetail( const std::vector<T*>& band )
{
for ( size_t i = 0; i < band.size(); ++i )
cout << "The details for " << band[i]->getName()
<< ": " << band[i]->print();
}
And there are many others, including iterators, functors, STL algorithms, etc.
You are trying to pass an object to an interface that wants a pointer.
void printInstrumentDetail(const I * const a)
Convert this to a reference.
void printInstrumentDetail(I const I& a)
But to conform to the pattern that is common in C++. You should pass the beginning and end of the sequence as parameters. ie change your function to take itertors rather than a pointer.
Instead of passing the pointer:
printInstrumentDetail(const I * const a)
you can pass the reference:
printInstrumentDetail(const I& a)
Everything else stays unchanged.
First of all, there seems to be no reason for PrintInstrumentDetail to be a template at all -- it works with pointers to the base class, and unless you're likely to have other types with getName() and print() members to which it might be applied, it can/could/should just work with pointers to the base class.
Second, I'd think hard about changing how you do the job. Instead of a member function in each Instrument, and PrintInstrumentDetail to loop over all the instruments, I'd think hard about defining operator<< for Instrument, and using a standard algorithm to print out the details.
Looking at it, I think a few other things should change as well. First of all, unless you're dealing with really unusual instruments, the number of valves on a brass instrument is fixed forever -- so it should NOT have a SetValve() member. Rather, the number of valves should be set during construction, but not be open to change afterwards.
String instruments don't have valves at all (at least most normal ones don't), so they shouldn't have SetValves(), GetValves(), or anything else related to valves.
Likewise, unless you're doing something pretty unusual, the cost of an instrument can never change -- you paid what you paid, so the cost should be set during construction, and not open to later alteration.
Edit: one other thing: instead of hard-coding 6 everywhere, use band.size() to loop over all the instruments in the band.