1

So I am very new at C++ and I have been given a task of creating a class along with its header file so test.h and test.cpp. Now I need to create a constructor that takes a vector of doubles as an argument and uses it to initialise the object. However, I am not able to figure out how to accurately do this. This is what I have added on my header and cpp class and I'm getting an error

Header file:

#include <string>
#include <vector>
using namespace std;
class Dog
{
    int age;
    string name;
    vector<long double> myVector;
public:

    Dog();   
    Dog(string name, int age, vector<long double> myVector);
};
#endif // DOG_H

cpp file:

using namespace std;
Dog::Dog()
{
   cout << "Dog object created"<< endl;   
}

Dog::Dog(string name, int age, vector<long double> myVector)
: name(name), age(age), myVector(myVector.push_back(myVector))
{
    cout << name<<" "<< age <<" "<<myVector<< endl;
}

now in my main class if i add this:

Dog d("Kelly",3,988);

and run the program I get the no match for operator error.

Tobias Wollgam
  • 761
  • 2
  • 8
  • 25
the feels
  • 107
  • 1
  • 7
  • 11
    `988` is not a vector. Do you want a vector that has a single element of `988` or a vector of size `988`? – NathanOliver Nov 14 '19 at 17:04
  • Also having the same name for vector member and for constructor parameter doesn't sound like a good idea for me. This: `myVector(myVector.push_back(myVector))` surely will be a problem... – alseether Nov 14 '19 at 17:08
  • The constructor needs a vector so you need to pass one when calling it. 988 is no vector and there is no implicit conversion to it. – Eric Nov 14 '19 at 17:10
  • @alseether having same name for constructor parameter and member is not a problem per se, though `myVector(myVector.push_back(myVector))` is certainly not correct – 463035818_is_not_an_ai Nov 14 '19 at 17:10
  • @formerlyknownas_463035818 true. I would replace *idea* for *practice* on my previous sentence – alseether Nov 14 '19 at 17:12
  • @alseether matter of opinion, most of my contructor parameters are not used anywhere but in the initializer list and then I find it wasteful to use a different name for it when there is no need – 463035818_is_not_an_ai Nov 14 '19 at 17:14
  • This is where you add `this->` to make things work as expected :) – Bktero Nov 14 '19 at 17:15

1 Answers1

3

You need to pass in the third parameter as a vector, or as something that can be used in the constructor of a vector.

If you want a vector with one element of 988 then use:

Dog d("Kelly", 3, { 988 });

This uses an initializer list which a vector can be constructed from.

This will highlight another problem, that the myVector member variable is not correctly initialised in the initializer list. Instead this should look something like:

Dog::Dog(string name, int age, vector<long double> v) : name(name), age(age), myVector(v)

This copies the elements in the parameter v into myVector.

It is also recommended not to have a using namespace std in header files. This is because anyone who #include <Dog.h> will end up with the std namespace at the top level. Check out this answer: https://stackoverflow.com/a/1453605/1517648

Steve
  • 7,171
  • 2
  • 30
  • 52