-2

I'm having some problems with passing vectors, as parameters, to a class function. Class is in another file. This is my code:

main.cpp:

#include <iostream>
#include <vector>
#include <string>

#include "class1.h"

using namespace std;

int main() {

    vector<string> names;

    names.push_back("Bob");
    names.push_back("Gorge");
    names.push_back("Bill");
    names.push_back("Freddy");
    names.push_back("Daniel");
    names.push_back("Emily");


    class1 obj;
    obj.printVector(names);

    system("pause");
}

class1.h:

#pragma once

class class1
{
public:
    void printVector(std::vector<string>& names);
};

class1.cpp:

#include <string>

#include "class1.h"

using namespace std;

void class1::printVector(vector<string>& names) {

    for (unsigned int i = 0; i < names.size(); i++)
    {
        cout << names[i] << endl;
    }
}

I tried doing the same thing with other data types (int, char, floats...) and it worked. ALso when i just pass them to a function in main.cpp it works. The errors are:

- Severity  Code    Description Project File    Line    Suppression State
Error   C3203   'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type    tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6

- Severity  Code    Description Project File    Line    Suppression State
Error   C3203   'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type    tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6

- Severity  Code    Description Project File    Line    Suppression State
Error   C2923   'std::vector': 'string' is not a valid template type argument for parameter '_Ty'   tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2923   'std::vector': 'string' is not a valid template type argument for parameter '_Ty'   tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2065   'string': undeclared identifier tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2065   'string': undeclared identifier tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.h  6   

- Severity  Code    Description Project File    Line    Suppression State
Error   C2664   'void class1::printVector(std::vector &)': cannot convert argument 1 from 'std::vector<std::string,std::allocator<_Ty>>' to 'std::vector &' tesst   c:\users\...\documents\programs and games\tesst\tesst\main.cpp  22  

- Severity  Code    Description Project File    Line    Suppression State
Error   C2511   'void class1::printVector(std::vector<std::string,std::allocator<_Ty>> &)': overloaded member function not found in 'class1'    tesst   c:\users\...\documents\programs and games\tesst\tesst\class1.cpp    9   

Please help! Thank You

Useless
  • 64,155
  • 6
  • 88
  • 132
jose2000
  • 3
  • 2

2 Answers2

1

class1.h does not include <string> or <vector>.

You're "getting away with" the former omission by the fact that you just so happen to include <string> in your source files before including class1.h (not something you should rely on!).

In the latter case, class1.cpp simply has no idea what vector means.

Fix your includes.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
-1

Your problem is not with passing parameters to a class. The following code works here:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class class1 {
public:
    void printVector(std::vector<string>& names);

};

void class1::printVector(vector<string>& names) {

    for (unsigned int i = 0; i < names.size(); i++)
    {
        cout << names[i] << endl;
    }
}

int main() {

    vector<string> names;

    names.push_back("Bob");
    names.push_back("Gorge");
    names.push_back("Bill");
    names.push_back("Freddy");
    names.push_back("Daniel");
    names.push_back("Emily");


    class1 obj;
    obj.printVector(names);
        return 0;
}

As mentioned before by Lightness Races in Orbit, your problem is not including vector and string to class1