![]() |
|
#1
|
|||
|
|||
Passing a C++ vector to functionWhat I'd like to do is create a C++ function that is passed a character array and a vector of long integers. What I'd like to know is how do I set up the Prototype, the Function definition and the actual call. I've tried the following without success (compiler errors):
the variables are defined as: CPP / C++ / C Code:
Prototype: CPP / C++ / C Code:
Call to function: CPP / C++ / C Code:
Function definition: CPP / C++ / C Code:
Any help with this will be appreciated Last edited by LuciWiz : 18-Apr-2005 at 09:53.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
|||
|
|||
|
Quote:
The function declaration (prototype) must be exactly equivalent to the function definition. You declared the second argument to be a pointer to a vector, but the definition has a reference to a vector. Normally, you should post the exact error messages to let us know what you have seen so far, but I'll just give an example to show how it should work: CPP / C++ / C Code:
Note that, for function arguments, "char *" and "char[]" are exactly equivalent, but "vector<long> &" and "vector<long> *" are really, realllllly different. Regards, Dave |
|
#3
|
|||
|
|||
|
Thanks Dave for your response.
I plugged in your suggestion into my compiler MS Visual Sudio .NET 2003 (MS Visual C++), and got the following compiler error: error C2783: 'void ProcessInput(const char *, std::vector<_Ty> *)' : could not deduce template argument for 'T' with [ _Ty=long ] The line it is flagging is the function call: ProcessInput(inCharArry, &inVec) Hope this provides the missing pieces |
|
#4
|
|||
|
|||
|
Quote:
I'm not sure what you are doing with the template<>. Unless someone else can see what you are getting at, maybe you should post more code. However, I would like to revisit my example. You should erase the following comment from your life-experience memory: Quote:
The statement is absolutely true for C, and most C++ compilers accept it, but strictly speaking you should make the definition match the declaration exactly. That's what it takes to get my example to compile with the .NET c++ compiler. CPP / C++ / C Code:
Regards, Dave |
|
#5
|
||||
|
||||
|
Quote:
Indeed, I compiled and ran Dave's code without problems (well, I did add the definition of the function before the main, because my compiler asked for it). The problem lies somewhere else. Please post the new code. Regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#6
|
|||
|
|||
|
Quote:
CPP / C++ / C Code:
|
|
#7
|
||||
|
||||
|
You pass the vector as a pointer. You can't call a method on a pointer to an object with .(dot). You need to use the -> construct:
CPP / C++ / C Code:
Regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#8
|
|||
|
|||
|
Quote:
That's the info I needed!!! I plugged in the "->" code, and the function is doing exactly what I want. Thank-you LuciWiz for your answer, and davekw7x, thanks for your help and quick response in getting me to my goal--- James |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| [Tutorial] Function Pointers | aaroncohn | C++ Forum | 4 | 17-Feb-2006 12:33 |
| Passing VARCHAR to function | enomad | C Programming Language | 5 | 29-Apr-2004 15:32 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 05:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The