GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Apr-2005, 09:03
SnackMan78 SnackMan78 is offline
New Member
 
Join Date: Feb 2005
Location: Wash. DC
Posts: 13
SnackMan78 is on a distinguished road

Passing a C++ vector to function


What 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:
	char inCharArry[100];
	vector<long> inVec;

Prototype:
CPP / C++ / C Code:
	void ProcessInput(const char *, vector<long>*);

Call to function:
CPP / C++ / C Code:
	ProcessInput(inCharArry, inVec);

Function definition:
CPP / C++ / C Code:
	template<long T>
	void ProcessInput(const char inChar[], vector<long>& invector)

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  
Old 18-Apr-2005, 09:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by SnackMan78
What 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):

Any help with this will be appreciated

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:
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int main()
{
  void ProcessInput(const char *, vector<long> *);

  char inCharArry[100];
  vector<long> inVec;

  cout << "Calling   function ProcessInput()" << endl;
  ProcessInput(inCharArry, &inVec);
  cout << "Back from function ProcessInput()" << endl;

  return 0;
}

void ProcessInput(const char inChar[], vector<long> *invector)
{
  cout<< "In function ProcessInput()" << endl;
}


Note that, for function arguments, "char *" and "char[]" are exactly equivalent, but "vector<long> &" and "vector<long> *" are really, realllllly different.

Regards,

Dave
  #3  
Old 18-Apr-2005, 09:51
SnackMan78 SnackMan78 is offline
New Member
 
Join Date: Feb 2005
Location: Wash. DC
Posts: 13
SnackMan78 is on a distinguished road
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  
Old 18-Apr-2005, 10:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by SnackMan78
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

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:
Originally Posted by davekw7x
Note that, for function arguments, "char *" and "char[]" are exactly equivalent

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:
int main()
{
  void ProcessInput(const char [], vector<long> *);

  char inCharArry[100];
  vector<long> inVec;

  ProcessInput(inCharArry, &inVec);

  return 0;
}

void ProcessInput(const char inChar[], vector<long> *invector)
{
  // all of the ProcessInput() stuff
}

Regards,

Dave
  #5  
Old 18-Apr-2005, 10:23
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by davekw7x
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.

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  
Old 18-Apr-2005, 12:53
SnackMan78 SnackMan78 is offline
New Member
 
Join Date: Feb 2005
Location: Wash. DC
Posts: 13
SnackMan78 is on a distinguished road
Quote:
Originally Posted by davekw7x
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:


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:
int main()
{
  void ProcessInput(const char [], vector<long> *);

  char inCharArry[100];
  vector<long> inVec;

  ProcessInput(inCharArry, &inVec);

  return 0;
}

void ProcessInput(const char inChar[], vector<long> *invector)
{
  // all of the ProcessInput() stuff
}

Regards,

Dave


CPP / C++ / C Code:
/* 
After compiling this, I get the error msg:
   [i][b]error C2228: left of '.push_back' must have class/struct/union type[/b][/i]
*/
 
//A better way to describe my intentions is that once I get the vector into 
//the ProcessInput function,  I want to populate the vector with values, and 
//have those values available when I leave the function for further processing.
//
//Since the vector dot.modifiers do not work on  my *inVector, I did not 
//explain my intentions clearly enough, and I described the  incorrect usage.  
//
//Ultimately, once I understand what I'm doing, I will change inCharArry to a 
//vector<char>, and let the User input as long a string as they desire
//
//I've ripped out everything associated with this program except what is 
//necessary for my problem. The template<T> reference is gone too
//

#include <iostream>
#include <vector>   
   using namespace std;

void ProcessInput(const char [], vector<long> *);
int conv2integer(char);

int main()
{	
	char inCharArry[100];
	vector<long> inVec;

	cout<<"\nEnter your first value: ";
	cin >> inCharArry;

	cout<<"\nThe input is: " << inCharArry;
	cout<<"\nThe numeric-only edited data is: \n";

	ProcessInput(inCharArry, &inVec);

  cout<<"\n";
  return 0;
}

void ProcessInput(const char inputVal[], vector<long> *inVector)
{
	int i = 0,
		nbr = 0;
	vector<long> inVec;

	while (inputVal[i] != '\0')
	{
		if(isdigit(inputVal[i]))
		{
			cout<< inputVal[i];
			nbr = conv2integer(inputVal[i]);	// convert the ascii input to integer values
			inVector.push_back(nbr);			//load vector with ONLY numeric input values
		}

	i++;

	}		// end while
}

int conv2integer(char inChar)
{
// Convert the ascii char to an integer, char function "atoi" does not work for this case. This is my work around

	switch(inChar)
	{
		case 49:
			return 1;
		case 50: 
			return 2;
		case 51:
			return 3;
		case 52:
			return 4;
		case 53:
			return 5;
		case 54:
			return 6;
		case 55:
			return 7;
		case 56:
			return 8;
		case 57:
			return 9;
		default:
			return 0;
	}
}
  #7  
Old 18-Apr-2005, 13:00
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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:
//inVector.push_back(nbr);      // wrong
inVector->push_back(nbr);      //load vector with ONLY numeric input values

Regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #8  
Old 18-Apr-2005, 13:29
SnackMan78 SnackMan78 is offline
New Member
 
Join Date: Feb 2005
Location: Wash. DC
Posts: 13
SnackMan78 is on a distinguished road
Thumbs up

Quote:
Originally Posted by LuciWiz
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:
//inVector.push_back(nbr);      // wrong
inVector->push_back(nbr);      //load vector with ONLY numeric input values

Regards,
Lucian


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 GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 04:49.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.