Using Vector in Class and Functions

Hello,

I am having some errors using a vector in my class definitions.
the errors i get are: variable or field `setName' declared void
expected `;' before '(' token
`vector' does not name a type

Can anyone give me an example of what the code is to look like?

Here is an example of what i have currently (only partial code):

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
#include <iostream>
#include <vector>
using namespace std;

const int MAX=10;

class Example
{
     private:
            vector <string> name(MAX);
     public:
            void setName(vector);
            vector getName();
};

//Function implementation
void Example::setName(vector <string> n)
{
     name(MAX) = n(MAX);    //I can tell from my errors this is incorrect
}


vector Example::getName()
{
     return name;
}

int main()
{
    //rest of my code
    
    return 0;
}
What on earth are you trying to do here: vector <string> name(MAX); Are you just trying to correct a vector of type string named name? I hope you aren't trying to put a size limit on vector. The whole point of using a vector is that you can resize it and change its size anytime you want. If you are trying to set a size you should just use an array.
Last edited on
ok so i will take out the (MAX), i thought that was needed to specify my size limit. I am trying to create a vector of type string in a private member variable. And I can't use an array because i am trying to learn how to use vectors.
Last edited on
Oh, if that's the case then all you need is vector<string> name; Also, you can't make a function that returns a type vector. The same way you can't make a function that returns the type array
Also void setName(vector); will never work. You would need something like void setName(vector<string>);

You should watch this video that explains how vector actually work.

https://www.youtube.com/watch?v=Cq1h1KPoGBU
Last edited on
So would I also make the function a type string?
like this: string Example::getName()

or was the orginal for getName() correct?

And thank you, i will watch the video.
Last edited on
The vctor class is a tejplated one and you therefore cant return vector. You should do something like return vector<std::string> and also for the functiin argument type. Creating the gector using MAX only creates the vector with MAX initial elements. It does not set any upper limit as vecyors are meant to be dynamic.

Aceix.
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
#include <iostream>
#include <vector>
#include <string>

class Example
{
     private:
            std::vector<std::string> names ;
     public:
            void set_all_names( std::vector<std::string> n );
            void add_one_name( std::string name );
            std::vector<std::string> get_all_names() const ;
};

void Example::set_all_names( std::vector<std::string> n ) { names = n ; }

void Example::add_one_name( std::string name ) { names.push_back(name) ; }

std::vector<std::string> Example::get_all_names() const { return names ; }

int main()
{
    Example e ;
    e.set_all_names( { "peter", "paul", "mary" } ) ;
    e.add_one_name( "john doe " ) ;

    const std::vector<std::string> vec= e.get_all_names() ;
    for( std::string n : vec ) std::cout << n << '\n' ;
}

http://coliru.stacked-crooked.com/a/21165ea548f959e4
Topic archived. No new replies allowed.