passing vector class to function

I have used a vector class inside a function like below to get every object in that class.
 
 std::vector<ClassName>student_detail(size);


Now I want to pass this vector class into another function and access class objects in that function. However, I don't what type to use?! if I pass it like this->
 
void function(std::vector<ClassName>& name)


I get multiple errors:

Error C2923 'std::vector': 'ClassName' is not a valid template type argument for parameter '_Ty'

Error C2065 'ClassName': undeclared identifier
Show full code. Are you attempting to reference ClassName before declaring ClassName?
I hope i could fit them here but its very long procedure. But this the main idea as below in header and cpp files:


.header file
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
class Sql : public mainClass

public:

    Sql();
    ~Sql();
    
void Sql::insertPerson(sqlite3*& sqldb, 
                                 sqlite3_stmt*& stmt, 
                                 int& student_count, 
                                 std::vector<Person>& student_detail);


/************************/

class Person : public mainClass

public:

    Person();
    ~Person();
        
   std::string getFirstName() const;
   void setFirstName();
   void entryFunction();



.cpp
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

Sql::Sql() {};
Sql::~Sql() {};

void Sql::insertPerson(sqlite3*& sqldb, 
sqlite3_stmt*& stmt, 
int& student_count, 
std::vector<Person>& student_detail){

           Sql sql;
           
           std::string query; 
            
          
           for(int i=0;i<student_count;i++)   {
               
                query.append(student_detail[i].getFirstName());
                .
                .
                .
                .
                .


          }
           
           char* err = NULL;
           int rc = sqlite3_exec(sqldb, query.c_str(), NULL, NULL, &err);
                if (rc != SQLITE_OK)
                    std::cerr << "\n\t\aERROR->" << err << std::endl;

}

/******************************/

Person::Person() {};
Person::~Person() {};

std::string Person::getFirstName() const {}
void Person::setFirstName(){}

void Person::entryFunction(){
  
   Sql sql;
   Person person;

   bool database_entry = false;
   int student_count = 3;
   std::vector<Person>student_detail(student_count);

   for(int i = 0; i< student_count; i++){

     student_detail[i].setFirstName();
    .
    .
    .
   

   }
            .
            .
            .
            .

             if(database_entry){
                      
                      sql.insertPerson(sql,stmt,student_count,student_detail);
                     
             }

           
}

Last edited on
Line 11 of first excerpt: The compiler doesn't know what Person is at this point.

Forward declare class Person before class Sql
1
2
3
4
5
class Person;

class Sql : public mainClass {
    // ...
};


PS: You should also be #including the necessary files, such as #include <vector> , #include <string?

PPS: If you have an empty constructor and destructor, it's a sign that you don't need to define these in the first place. See: "rule of 0". And even if you have custom logic in the constructor, you still only need a destructor if you need to clean up some non-RAII resources.
Last edited on
Perfect, That was the right solution!
I moved Sql to the very end of header and cpp and now no-errors.

All libraries are included, I only made it simple here just to show you to related classes and functions for this problem.

Thanks, I didn't know about "rule of 0". I'll look into it :)


All good, the reason I brought up the #includes is that (in general) it might be important to show them when you are running into errors like "undeclared identifiers".
Topic archived. No new replies allowed.