Vector of class type initilization

closed account (EwCjE3v7)
Okay I do not know how I can initlize Sales_data items that are in a vector.

This is what I`m trying to do

vector<Sales_data> items = {0-235-1456, 0-159-1587, 0-218-1548, 1-168-1877};

But its giving me this error
Testing.cpp:40:76: error: could not convert ‘{-1691, -1746, -1766, -2044}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<Sales_data>’
  vector<Sales_data> items = {0-235-1456, 0-159-1587, 0-218-1548, 1-168-1877};


Here is the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <iostream>
#include <string>

class Sales_data; // declaration for the following class

std::istream &read(std::istream &, Sales_data &); // declaration for read, used inside the class

class Sales_data {
public:
  friend std::istream &read(std::istream &, Sales_data &);
  friend std::ostream &print(std::ostream &, const Sales_data &);
  friend Sales_data add(const Sales_data &, const Sales_data &);

  // CONSTRUCTORS
  Sales_data() = default; // needed

  Sales_data(const std::string &s, unsigned n, double p) : bookNo(s), units_sold(n), revenue (p*n) {} // if we get a string, unsigned and a double then initialize the members of the class

  Sales_data(const std::string &s) : Sales_data(s, 0, 0) {} // if we just get a string as a initializer then assign that to bookNo

  Sales_data(unsigned n, double p) : Sales_data("", n, p) {}


  Sales_data(std::istream &is) // if we get an istream
  {
    read(is, *this); // call read to assign the current object
  }


  std::string isbn() const // funtion to call for the ISBN of the current book
  {
   	 return bookNo; // return the book numbers
  }

  Sales_data& combine (const Sales_data&rhs) // to combine two Objects of Sales_data
  {
  	units_sold += rhs.units_sold; // add the amount of books sold
  	revenue += rhs.revenue;       // for adding the revenue
  	return *this;                 // return the object that the funtion was called on
  }

	inline double avg_price() const // for calculating the average price
  {
		if (units_sold)
      return revenue/units_sold; // return: devide revenue by the amount of units sold
  	else
  	 return 0;                   // else return nothing
  }

private:
  std::string bookNo;      // for holding our book number/ISBN
  unsigned units_sold = 0; // for holding the amount of books sold
  double revenue = 0.0;    // for holding the price they were sold at
};


std::istream &read(std::istream &is, Sales_data &item) // funtion for getting input into a Sales_data object
{
  double price = 0;                              // for getting our price that the objects were sold at
  is >> item.bookNo >> item.units_sold >> price; // get input
  item.revenue = price * item.units_sold;        // calculate the revenue
  return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item) // funtion for output
{
  os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); // print out values
  return os;        // return the output stream
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs) // for adding to Sales_data objects
{
  Sales_data sum = lhs; // sum for returning
  sum.combine(rhs);     // combine sum with rhs
  return sum;           // return the combined object
}

#endif // SALES_DATA_H 
This is an integer expression: 0 - 235 - 1456 that evaluates to const integer literal -1691.
This is a const C-string literal: "0-235-1456". It could convert to a std::string.
The first thing you will need to do is use the correct type of variables that match one of your constructors. You have three constructors that take the following type of parameters:

string, unsigned, double
string
unsigned, double

But you appear to be trying to use a signed value in your initialization list. Remember 0-235-1456 is a mathematical formula that will be completed before the constructor is executed 0 minus 235 is minus 235 minus 1456 is minus 1691 (-1691) just as the error message shows. If you want to use as string you'll need to surround the value with quotes.

You'll probably also need to surround each individual element with another pair of braces.


closed account (EwCjE3v7)
Thank you, how stupid of me. Thanks
Topic archived. No new replies allowed.