user5579852
user5579852

Reputation:

Passing a Vector made of a Class to a function (C++)

I'm running into a lot of problems with this, primarily being that my passed Vector of my Class keeps getting flagged as an undeclared identifier. Any help on solving the problem or explanations to help me figure out what I don't understand would be greatly appreciated.

So here is a simplified version of what I have now:

main.cpp

#include "functions.h"
#include "vehicle.h"

int main()
{
   int menuSelection;

   vector<Vehicle> inventory;

   do
   {
        cout << "Please make a selection:" << endl << endl;
        cout << "1:   Display Inventory" << endl;
                     .......
        cout << "8 : Write inventory to file and exit" << endl << endl;

        if (menuSelection == 1)
        {
            if (inventory.empty())
                cout << "Database empty" << endl;
            else
                display(inventory);
                     .......
    } while (menuSelection != 8);

    return 0;
}

vehicle.h

#pragma once
#include "functions.h"

class Vehicle
{
   private:
         string VIN;
         int year;
                    .......
//   public:
//        string getVIN();
                    .......
}

functions.h

#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void display(vector<Vehicle>);
                    .......

void display(vector<Vehicle>& in)
{
        Vehicle v;
        cout << "VIN: " << v.getVIN(in)
}
                    .......

I've tried a bunch of different things to get it to work so that's why a lot of stuff may seem like odd syntax (I'm also not very good). My assignment is to have a menu in main.cpp which will create a vector from a class stored in vehicle.h, and then the menu is supposed to call functions which are located in functions.h that will communicate through vehicle.h to a fourth not included vehicle.cpp to work with information from the class.

Upvotes: 1

Views: 130

Answers (2)

M.M
M.M

Reputation: 141613

In functions.h, void display(vector<Vehicle>); does not compile because Vehicle is undeclared at this point.

Also, in functions.h, void display(vector<Vehicle>& in) is a different overload to the previous prototype (the & makes a difference), probably not what you intended. And then you place a function body in functions.h -- this should not be there.

You need to organize your code so that Vehicle class definition appears, and then functions.h includes that.


So vehicle.h should look like:

#pragma once
#include <string>
// do NOT include functions.h

class Vehicle
{
  // ...
};

and then functions.h should look like:

#pragma once
#include "vehicle.h"
// do NOT do "using namespace std;" in a header and don't include any unnecessary headers

void display(vector<Vehicle> &in);

and then functions.cpp should #include "functions.h" and contain the function body for display.

Upvotes: 1

Itamar Katz
Itamar Katz

Reputation: 9655

undeclared identifier means the compiler sees a name but don't see a declaration of that name (as a variable, class, function, etc.). It happens in your code in a few places, for example:

  • MyClass is used but not declared (main.cpp)
  • Vehicle is used but not declared (functions.h)
  • You use Vehicle.v but v not declared in class Vehicle (in addition, I doubt that this is what you intended - if you use class name Vehicle.v it means accessing a static variable, as opposed to vehicle.v).

It seems you lack some basic background. The most important thing is that you learn to decipher compiler errors, so at least you understand what went wrong.

Upvotes: 0

Related Questions