Compiler tells me im trying to convert a string to int?

Ok so i put a pointer to a vector in my Customer class and now its giving me 2 errors


Customer_Class.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CUSTOMER_CLASS_H_INCLUDED
#define CUSTOMER_CLASS_H_INCLUDED

#include <string>
#include <vector>

class Customer
{
    public:
        Customer(std::vector<std::string> CUSTOMERNAME);
        ~Customer();

        std::vector<std::string> *p_customerName = new vector<std::string>;
        std::vector<std::string> customerName;
};

#endif // CUSTOMER_CLASS_H_INCLUDED 


C:\Users\Chay Hawk\Desktop\Gun Shop\Customer_Class.h|13|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Users\Chay Hawk\Desktop\Gun Shop\Customer_Class.h|13|error: expected type-specifier before 'vector'|
C:\Users\Chay Hawk\Desktop\Gun Shop\Customer_Class.h|13|error: cannot convert 'int*' to 'std::vector<std::basic_string<char> >*' in initialization|
C:\Users\Chay Hawk\Desktop\Gun Shop\Customer_Class.h|13|error: expected ';' before 'vector'|
||=== Build finished: 3 errors, 1 warnings (0 minutes, 0 seconds) ===|
> so i put a pointer to a vector
¿why?

> error: expected type-specifier before 'vector'
it is std::vector
Hi Chay,

As the warning says: if not using c++11, you can't initialise variables in the declaration, unless the variable is static.

Apart form that, it is probably not a good idea (or no need) to have a pointer to an STL container. Just use a reference instead.

When using new, whatever type you have on the right of new has to match the type of the variable on the left side of =. But it is probably best to steer clear of pointers.

Why are you sending a std::vector<std::string> as an argument (in the constructor) to your Customer object? Perhaps the Customer should HAVE these as member variables?

So I would recommend that you decide what data your customer should store, have those as member variables, then create an interface of public functions.

Hope all goes well.
"¿why?"

Why not?
Why not?

Because you give yourself extra work to do.

Now you will have to "customize" your copy constructor and assignment operator to ensure that your Customer class doesn't do a shallow copy of the vector.

If you go with C++11 you may need to look into the move constructor and move operator as well (although I think any decent compiler should automagically create them correctly).

And then, the extra burden of delete'ing your vector pointer, this is best done in the destructor.
Further to my last post, there is the concept of a class just for the data (Customer class), and then separately a container to put the data objects in (All the customers). This could be an STL container in main() or even another class.

I mentioned this here:

http://www.cplusplus.com/forum/lounge/111525/


But I didn't get any comment either way.

Have brought this up because of the OP's use of a vector of strings and pointers, implies (well to me anyway) a bit of confusion about this idea.

HTH
so what would i use a pointer in a class for? an array?
As far as I know, in modern C++ there is never a reason to use a real raw pointer except in extreme cases.
Last edited on
Topic archived. No new replies allowed.