| | |
Passing array between functions
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 26
Reputation:
Solved Threads: 1
Hi!. I am having some problems with functions scopes again. I need to use one array initialized in one function, in a different function. I have searched on how to pass the array by reference or value ( and I truly don't know if that's what I need) but I really don't understand it.
This is only part of my code. If you need the rest I will post it:
As you can see I need to use the stats[4] array in the retrieveSavedata function in the outputSavedata function. But when I try to initializeit, it gives me this error:
'stats' not declared in that scope
Any help is appreciated,
AssaultM16
This is only part of my code. If you need the rest I will post it:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include "Savedata.h" using namespace std; void outputSavedata (int stats[4]) { cout<< "\nStrengh:" << stats[0] << "\n"; cout<< "Defense:" << stats[1] << "\n"; cout<< "Hitpoints:" << stats[2] << "\n"; cout<< "Experience:" << stats[3] << "\n"; cout<< "Level:" << stats[4] << "\n"; } void retreiveSavedata (string filename) { int stats[4]; ifstream file(filename.c_str()); string skipline; getline(file, skipline); file>> stats[0]; file>> stats[1]; file>> stats[2]; file>> stats[3]; file>> stats[4]; outputSavedata(stats); file.close(); }
As you can see I need to use the stats[4] array in the retrieveSavedata function in the outputSavedata function. But when I try to initializeit, it gives me this error:
'stats' not declared in that scope
Any help is appreciated,
AssaultM16
•
•
Join Date: Feb 2008
Posts: 630
Reputation:
Solved Threads: 46
You have to create the array in the scope that you are going to use it! Probably the way to go here is have the retrieve() function accept a pointer to the array it will fill. Also, be careful not to use element 4 of an array[4] - it has 4 elements, 0,1,2,3!
C++ Syntax (Toggle Plain Text)
#include <iostream> void retreiveSavedata (int* stats); void outputSavedata (int stats[4]); int main() { int stats[4]; retreiveSavedata(stats); outputSavedata(stats); return 0; } void outputSavedata (int stats[4]) { std::cout<< "\nStrengh:" << stats[0] << "\n"; std::cout<< "Defense:" << stats[1] << "\n"; std::cout<< "Hitpoints:" << stats[2] << "\n"; std::cout<< "Experience:" << stats[3] << "\n"; } void retreiveSavedata (int* stats) { stats[0] = 0; stats[1] = 10; stats[2] = 20; stats[3] = 30; }
•
•
Join Date: Jul 2009
Posts: 26
Reputation:
Solved Threads: 1
Thanks David. Maybe you can now help me with another problem. I did exactly like that and its now giving me "undefined reference to `retrieveSavedata(int*, std::string)"
Heres the code:
Savedata.h
Savedata.h
I believe I have correctly defined the retrieveSavedata function so I really don't why the compiler is giving me this error.
Also why do I need to use a a pointer to the stats variable for the outputSavedata function?. Couldn't I do this:
Thanks,
AssaultM16
Heres the code:
Savedata.h
C++ Syntax (Toggle Plain Text)
#ifndef SAVEDATA_H #define SAVEDATA_H #include <string> using namespace std; void retrieveSavedata (int stats[4], string filename); void outputSavedata (int* stats); #endif
Savedata.h
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include "Savedata.h" using namespace std; void retreiveSavedata(int stats[4], string filename) { ifstream file(filename.c_str()); string skipline; getline(file, skipline); file>> stats[0]; file>> stats[1]; file>> stats[2]; file>> stats[3]; file>> stats[4]; file.close(); } void outputSavedata (int* stats) { cout<< "\nStrengh:" << stats[0] << "\n"; cout<< "Defense:" << stats[1] << "\n"; cout<< "Hitpoints:" << stats[2] << "\n"; cout<< "Experience:" << stats[3] << "\n"; cout<< "Level:" << stats[4] << "\n"; }
I believe I have correctly defined the retrieveSavedata function so I really don't why the compiler is giving me this error.
Also why do I need to use a a pointer to the stats variable for the outputSavedata function?. Couldn't I do this:
C++ Syntax (Toggle Plain Text)
outputSavedata(int stats[4])
Thanks,
AssaultM16
•
•
Join Date: Feb 2008
Posts: 630
Reputation:
Solved Threads: 46
1) The undefined reference is because you are not linking to SaveData.cpp. Which IDE are you using (Visual studio, etc)?
2) 'retreive' should be spelled 'retrieve'.
3) you seem to still be using the 5th element (element 4), when it is not valid.
4) it seems to work just fine when you change
2) 'retreive' should be spelled 'retrieve'.
3) you seem to still be using the 5th element (element 4), when it is not valid.
4) it seems to work just fine when you change
void outputSavedata (int* stats); to void outputSavedata (int stats[4]); ![]() |
Similar Threads
- Passing 2D Array of Pointers into a function (C++)
- Passing An Array Of Structures To A Function (C)
- passing arrays to functions (PHP)
- Passing an Array to Function Problem (C++)
- arrays and pointers (C++)
- Re: acessing variables (C)
- passing strings as functions (C++)
- Passing array of structures into a function (C++)
Other Threads in the C++ Forum
- Previous Thread: How to get textbox value of external process
- Next Thread: Inkey input control
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





