I have a function called generate_all_paths, defined as such:
template <int size>
void generate_all_paths(vector<string> maze[][size], int x, int y) {
....
}
I am trying to call it in my main function as so:
int main() {
string s;
ifstream mazefile("maze.txt");
if (!mazefile) {
cout << "File not found. Please try again." << endl;
}
while (getline(mazefile, s)) {
mazevec.push_back(s);
}
generate_all_paths(mazevec, 0, 1);
return 0;
}
where mazevec is vector<string> mazevec;
But my IDE says that my call to generate_all_paths in main does not match the function definition. I'm a little confused why this is happening. mazevec is a vector string, so shouldn't the parameter data types match up?
The mazevec you are passing to the function is a vector<string>. Your function definition indicates that it expects a 2D vector array. In your function prototype, change it to this:
void generate_all_paths(vector<string> maze, int x, int y);
This should work.
You'll have to pass an array but you have passed a variable that is not an array. So they are treated as two different functions.
Please pass an array of type vector and try again.
Related
void print(string str,int a=0)
{
cout<<str;
}
int main()
{
string str="hello world";
print(str);
return 0;
}
why is the code working if I'am only passing one argument whereas the function needs two arguments
Whenever you have a function with a defaulted argument
void some_func(int a, int def = 0)
{
//something
}
The following call
some_func(42);
is converted into
some_func(42, 0);
And you can also call the function with two arguments, such as some_func(42, 1);
It works because you defined this int on a function declaration. The compiler knows that a =0 and is an int type. However if you lets say call this function like this:
print(str,10);
int a will have value of 10 instead of 0.
You are declaring function with one argument to have a default initial value, that is why you are able to call function without that argument.
if not passing that argument to function, its initial value that you declared, is used.
I am having trouble matching up data types for a function I have written,
The function is:
void generate_all_paths(int size, char *maze[][size], int x, int y) {
...
}
This parameters size, x, and y are all super simple. I believe it is the maze that is throwing me off. It is intended to be a multidimensional size x size array, containing characters of the alphabet which acts like a maze.
When I try to call the function in main as such:
int main() {
char *exmaze[][6] = { {"#","#","#","#","#","#"},
{"S","a","#","h","l","n"},
{"#","b","d","p","#","#"},
{"#","#","e","#","k","o"},
{"#","g","f","i","j","#"},
{"#","#","#","#","#","#"}
};
generate_all_paths(6, *exmaze, 1, 0);
return 0;
}
My IDE complains that there is no generate_all_paths function with matching data types for its parameters.
I am fairly certain that my problem is in main where I defined exmaze but my tweaks were unable to fix it.
Does anybody have any suggestions? Thank you!
*exmaze - why the dereferrence? generate_all_paths(6, exmaze, 1, 0) will pass the pointer by value - which is I suppose what you want in this case.
You haven't shown what's size, but just make sure it's compile-time known constant.
Also, questions like this almost always get recommendations to use standard containers like std::vector so I won't miss it.
In my opinion, using a template is the most elegant way for this:
template<int size>
void generate_all_paths(const char *maze[][size], int x, int y) {
...
}
int main() {
const char *exmaze[][6] = { {"#","#","#","#","#","#"},
{"S","a","#","h","l","n"},
{"#","b","d","p","#","#"},
{"#","#","e","#","k","o"},
{"#","g","f","i","j","#"},
{"#","#","#","#","#","#"}
};
generate_all_paths(exmaze, 1, 0);
return 0;
}
Please also note the const char[][]!
I'm sort of new to C++ and programming in general. I'm making a pokemon remake of the old gameboy version for fun, and I'm having trouble passing a whole structure as an arguement.
This is a shortened version to highlight the problem I'm having:
struct Enemy_Pokeman
{
string opp_name;
int num_pokeman;
int pokeman_LVL;
};
void pl_Pokeman(Enemy_Pokeman);
void pokeman_data(string opp_name, int num_pokeman, int pokeman_ID[], int pokeman_LVL[],
int purpose)
{
Enemy_Pokeman enemypokeman[num_pokeman];
enemypokeman[0].opp_name = opp_name;
enemypokeman[0].num_pokeman = num_pokeman;
for(int i=0; i<num_pokeman; i++)
enemypokeman[i].pokeman_LVL = pokeman_LVL[i];
pl_Pokeman(enemypokeman); //Function call - Codeblocks detects error
//on this line
}
void pl_Pokeman(Enemy_Pokeman enemy)
{
cout << endl;
}
Sorry if this doesn't make sense, I didn't want to post the entire thing, so I chopped it up a bit.
The problem is that it won't accept Enemy_Pokeman as an arguement.
Function pl_Pokeman only takes Enemy_Pokeman type while you passed in an array of Enemy_Pokeman
You update pl_Pokeman function to take array as input:
void pl_Pokeman(Enemy_Pokeman enemy[], int arraySize);
Or
template<typename T, size_t N>
void pl_Pokeman(Enemy_Pokeman (&enemy)[N])
you're passing to your function whole array of Enemy_Pokemans, not just one element. function expects one element only. also, you're creating that array within a function, so it's a local variable. if function pokemon_data returns, that array will be destroyed.
For Single structure-
When you are passing the structure as a argument, you should pass with & operator.
pl_Pokeman(&enemypokeman); // Fix 1
While catching it you need to catch it with Structure pointer.
void pl_Pokeman(Enemy_Pokeman *); // Fix 2
For Array of structure-
pl_Pokeman(&enemypokeman,size); // pass it with size
while catching it
void pl_Pokeman(Enemy_Pokeman (*)[], int );
My code is already working, seen here: http://pastebin.com/mekKRQkG
Right now, my functions work but utilizing information that I've declared globally, I guess, and I want to convert them so that they are in the format as seen on lines 11-15, but I'm unsure of how to convert them to do so. Simply put, I'm trying to convert my function of
"void add_county_election_file"
to be in the format of
"void add_county_election_file(const string, const vector &, const vector &, const vector &, const vector &)"
and I have no idea where to begin or how to even start.
Could someone please help me out and show me how I'd do this for the first function, so I can implement it across the board?
Thanks guys!
Your function declaration should look something like this:
void add_county_election_file(const string, vector<int>&, vector<string>..);
Make sure that your argument list for the vector template is correct(that's the type you put between <>)
Then match the implementation of you function to the declaration:
void add_county_election_file(const string, vector<int>&, vector<string>..){...}
Now call your function with apppropriate arguemtns in main:
string s;
vector<int> arg;
vector<string> sv;
void someFunction (s, arg, sv ...);
I think you are doing correct as the function you have declared
void add_county_election_file(const string, vector<int>&, vector<int>&,..);
so you just have to call the above function with the required arguments, as right now you are not passing the argument and your current definition doesn't accepts any arguments.
And as a good practice, in your int main() function you can use switch rather than going for if else.
Store your variables and functions in a class, overload operators and create functions to access these variables.
Declare all variables in int main() and set parameters to be passed into each function e.g.
void print_results() is modified to become
void print_results(std::vector<int> vec, int nCount, etc..)
Similar to the first one, create a struct to hold all data members, then pass the struct(by ref) into each function.
struct CountryTracker
{
std::vector<int> ID;
std::string name;
//etc...
}
`void print_results(CountryTracker& Obj) //pass single struct into functions`
The OOP way to do this is to create a class called perhaps ElectionInfo, where:
These would be its member fields:
vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;
and these would be its member functions:
void add_county_election_file(const string);
void search_county(const string);
void print_results();
This way you don't have to pass the references to the vectors around at all, instead you can just do:
ElectionInfo an_elect_info;
char selection = get_menu_choice();
// some if-statements to decide which of the following to call:
an_elect_info.add_county_election_file(county_name);
an_elect_info.search_county(county_name);
an_elect_info.print_results();
But if you'd prefer to stay with the current functional approach:
Declare and initialize the following inside your main method:
vector <string> countyNameVector;
vector <int> countyNCount;
vector <int> countyFCount;
vector <int> countyOCount;
int NCount;
int FCount;
int OCount;
int NTotal;
int FTotal;
int OTotal;
The syntax for the commented out function declarations should be tweaked to look like this:
void add_county_election_file(const string, vector<string>&, vector<int>&, vector<int&, vector<int>&);
(Of course, the definition should follow suit)
You would invoke it like this:
add_county_election_file(countyname, countyNameVector, countyNCount, countyFCount, countyOCount);
Objects are automatically passed-by-reference.
The basic process of refactoring should at the first step involve only code grouping and placement and should only minimally involve writing new logic. Using this as a principle you can go about modifying the code in the following way at first.
string ReadInputString(const char* title)
{
string s
cout << title;
cin >> s;
}
void add_county_election_file(const std::string& filename
, std::vector<string>& countyNameVector
, std::vector<int>& countyNCount
, std::vector<int>& countyFCount
, std::vector<int>& countyOCount
)
{
int NCount = 0;
int FCount = 0;
int OCount = 0;
int NTotal = 0;
int FTotal = 0;
int OTotal = 0;
char vote;
std::ifstream input((filename).c_str());
string countyName;
if(input.is_open())
{
input >> countyName;
countyNameVector.push_back(countyName);
while(input >> vote)
{
if(vote == 'N' || vote == 'n')
{
NCount = NCount + 1;
}
else if(vote == 'F' || vote == 'f')
{
FCount = FCount + 1;
}
else
{
OCount = OCount + 1;
}
}
countyNCount.push_back(NCount);
countyFCount.push_back(FCount);
countyOCount.push_back(OCount);
}
cout << countyName << endl;
}
void add_county_election_file()
{
string fn = ReadInputString("Enter the county file to process: ");
add_county_election_file(fn,g_countyNameVector,g_countyNCount,g_countyFCount,g_countyOCount);
}
As you can see I have just extracted your code and moved them to individual functions and changed names to make some significance. Like in the function ReadInputString - the line "cin >> s" was originally "cin >> filename". The abstract name "s" is to signify that the ReadInputString has no knowledge or doesn't care what the semantic meaning of the string it is reading from console.
In order to not change your main function - I have added a overloaded add_county_election_file that calls one function followed by another. The idea is that you should keep something unchanged and change others (for good) and then alternate if need be.
And I have changed names of your global variable to differentiate them from the local variable using "g_" - the point is that "g_" should only be found at very few places in your code.
I am writing a small game for an assignment and it requires using a map, I have successfully put the map into a 2d array, but now working further into the assignment I found I need to access the array Map[][] in another function. I have tried to get it to work but failed. The error I get with g++ is " error: 'Map' is not a type " Any help would be appreciated.
I have searched but either I am terrible at using the search engine or I couldn't find anything specific to this error.
const int MapSZ = 10; //In Global
int Map[MapSZ][MapSZ]; // Also Global
void GetMap(ifstream&, int); //Getting the map (Proto)
GetMap(fin, Map[MapSZ][MapSZ]); //In the main function.
void GetMap(ifstream& fin, Map[MapSZ][MapSZ]) //Inserting the map into an array
void GetMap(ifstream& fin, Map[MapSZ][MapSZ])
should be:
void GetMap(ifstream& fin, int Map[MapSZ][MapSZ])
^^^^
Notice, that Map is the name of the array, but you didn't mention its type.
If Map[MapSZ][MapSZ] is defined as a global, as your comment states (i.e. it is defined in main.cpp but outside of the main function), there is no need to pass it as a parameter to GetMap. You could simply do something like
void GetMap(ifstream& fin); //proto
int main(int argc, const char * argv[]) {
GetMap(fin);
}
void GetMap(ifstream& fin) {
//some code that uses Map[MapSZ][MapSZ]
}