Sherifftwinkie
Sherifftwinkie

Reputation: 391

Passing Object Class array a strings to function

I've been looking around for a fix to my errors but I can't quite find my case anywhere. So I have a function

int getUniques(Visitors info[], string address, string startDate, string endDate);

The function is going to take in my array full of Class Objects, and 3 strings. When I attempt to build, I get an assortment of errors. I'll list them all.

'Visitors' was not declared in this scope

Expected primary-expression before 'address' (same goes for other strings)

Expression list treated as compound expression in initializer

Anyone know what these mean and how to fix them?

EDIT: Lol I know so stupid of me, I fixed it now but I have other issues that I wanted to lead into. I am going to re-do my post and re-post in a little while if I still can't get my method to work.

Upvotes: 0

Views: 166

Answers (2)

lvella
lvella

Reputation: 13413

Those errors means the names Visitors and string were not recognised as types by C++ compiler...

In order to work, the declaration of your type Visitors must preceed the function declaration; either in the same source file or in a header file included with #include pre-processor directive.

Also, C++ string type is defined in the standard header file string, that must be included prior to usage, and the type is cotained in the namespace std, so you either need to use the full name std::string or declare you are using it with the using directive:

#include <string>
#include "visitors.hpp"

using std::string;

int getUniques(Visitors info[],
               string address,
               string startDate,
               string endDate);

and in the visitors.hpp file, your type definition (proably a class, but could also be a struct, an union, an enum or a typedef to some other type)...

#pragma once

class Visitors {
     ... // Your class definition.
};

Note that it is a bad practice to use the using directive in header files, because it will pollute the default namespace of the compilation units that include your header. If that is the case, you should use the full name std::string.

Upvotes: 2

Alec
Alec

Reputation: 92

Is "Visitors Info" the declaration of an array being passed to the function?

If so, you need to declare what type of variable it is. eg. An integer array, or string array

So a corrected function declaration could be.

int getUniques(string Visitors_info[], string address, string startDate, string endDate);

Assuming I'm interpreting what your code is trying to do correctly.

Upvotes: 0

Related Questions