Simmilar problem again using a vector of length 4

vector <char> something;
 something += '1','2','3','4';


 somefunction(something,4);

some function takes in a

char* ptr, unsigned int size

at the moment i am getting
"error C2664: 'something' : cannot convert parameter 1 from 'char' to 'char *"
any help would be appreciated even if i am not doing the obvious i have tried looking online even adding
char* psomething = something;
but that doesnt work well gives me more error messages

Recommended Answers

All 9 Replies

>>Simmilar problem again using a vector of length 4
Similar to what? What are you trying to do? If you're trying to pass a vector to a function, it appears that you have not defined your arguments/parameters correctly.

To pass a vector to a function, you should define the parameter as a reference to a vector (making the reference constant when it makes sense to do so), not an individual object of a specific dataType.

Additionally, unlike an array, it is not necessary to pass a "size" type variable along with it. A vector has all of that information built in to it, you just need to use the correct member methods to get to it.

>>Simmilar problem again using a vector of length 4
Similar to what? What are you trying to do? If you're trying to pass a vector to a function, you have not defined your arguments/parameters correctly.

To pass a vector to a function, you should define the parameter as a reference to a vector, not an individual object of a specific dataType.

How would i provide a reference ? I have also not provided the exact code i am using due to not being able to.
and it was similar to a question asked a couple of days ago - be nice i have a stinking cold aching shivering and anything and trying get my head around pointers only just cracked arrays :?:

I just checked the documentation for the vector class and it appears that <vector> does not offer an overloaded += operator. It seems that only the = assignment and [ ] subscript operators are offered.

It looks as though you are trying to push char type variables into your vector. Probably the common vector class member function you will use is push_back()

for(char c = '1'; c < '5'; c++)
{
     //This is how you put stuff in a vector
     something.push_back(c);
}

The vector class does offer a [ ]subscript operator which we can use to access the individual elements of the vector array:

for(unsigned int i=0, size=something.size(); i<size; i++)
{
     //whatever this function does, we will iterate through all elements of the something vector and pass each char into the function
     //Your function requires a char pointer
     somefunction(&something[i], i);
}

Not entirly sure if it makes a differnace but i am using

#include <boost/assign/std/vector.hpp>

the += usually works for my previous programs may try your second method

I just checked the documentation for the vector class and it appears that <vector> does not offer an overloaded += operator. It seems that only the = assignment and [ ] subscript operators are offered.

It looks as though you are trying to push char type variables into your vector. Probably the common vector class member function you will use is push_back()

for(char c = '1'; c < '5'; c++)
{
     //This is how you put stuff in a vector
     something.push_back(i):
}

Since the vector class does offer a [ ]subscript operator, which we can use to access the individual element of the vector array:

for(unsigned int i=0, size=something.size(); i<size; i++)
{
     //whatever this function does, we will iterate through all elements of the something vector and pass each char into the function
     //Your function requires a char pointer
     somefunction(&something[i], i);
}

You can try using vector.begin() as a pointer to a first element of table and use it as normal char* (but I didn't test it). You can make your function declaration fun(vector& v) and use it fun(v).

//edit: you can't pass iterator as pointer, but &*vector_name.begin() should work

>>it was similar to a question asked a couple of days ago
I figured as much, but only those involved in that thread (myself NOT included) will know what you're talking about.

>>be nice i have a stinking cold aching shivering and anything and trying get my head around pointers only just cracked arrays
Not real sure where the "be nice" came from, but okay...

Do you know how passing by reference works for an int, for example? Passing a vector by reference works the same way, you just need to say that you're passing a vector of _________ (fill in the blank) values to the function instead of a single value.

Something like this:

#include <iostream>
#include <vector>

using namespace std;

void populateVector(vector<int> &);

int main() {
  vector<int> myVector(10, 0);
  populateVector(myVector);
  for (int i = 0; i < myVector.size(); ++i) {
    cout << "Element " << i << " is " << myVector[i] << endl;
  }
  return 0;
}

void populateVector(vector<int> &localVector) {
  for (int j = 0; j < localVector.size(); ++j) {
    localVector[j] = (j + 1);
  }
}

You could also use vector::push_back() as Clinton mentioned. That would require changes to the declaration and the for loop in populateVector(). The method I have demonstrated, creates all elements of the vector before passing instead of after.

Not entirly sure if it makes a differnace but i am using

#include <boost/assign/std/vector.hpp>

the += usually works for my previous programs may try your second method

I would not recommend using such third-party extension functionalities until you are first familiar with how to use the vector class properly on its own.

commented: good well written example +6

>>it was similar to a question asked a couple of days ago
I figured as much, but only those involved in that thread (myself NOT included) will know what you're talking about.
>> Sorry

>>be nice i have a stinking cold aching shivering and anything and trying get my head around pointers only just cracked arrays
Not real sure where the "be nice" came from, but okay...
>> your first reply came across a bit harsh :)

Do you know how passing by reference works for an int, for example? Passing a vector by reference works the same way, you just need to say that you're passing a vector of _________ (fill in the blank) values to the function instead of a single value.

Something like this:

#include <iostream>
#include <vector>

using namespace std;

void populateVector(vector<int> &);

int main() {
  vector<int> myVector(10, 0);
  populateVector(myVector);
  for (int i = 0; i < myVector.size(); ++i) {
    cout << "Element " << i << " is " << myVector[i] << endl;
  }
  return 0;
}

void populateVector(vector<int> &localVector) {
  for (int j = 0; j < localVector.size(); ++j) {
    localVector[j] = (j + 1);
  }
}

You could also use vector::push_back() as Clinton mentioned. That would require changes to the declaration and the for loop in populateVector(). The method I have demonstrated, creates all elements of the vector before passing instead of after.


I would not recommend using such third-party extension functionalities until you are first familiar with how to use the vector class properly on its own.

>> Unfortunately i have to use it
Thank you for your help the main problem i am having is applying the vector to the function my vector is of type char and my function takes in char* how can i convert it ??
i have tried

vector<char*>

but that doesn't seem to work any suggestions ???

Did you write the function, or were you given the function and told to make it work?

If you wrote it, the easiest solution would be to change the definition of the function's parameter.

If you didn't write it, you have to change the declaration of your vector. The declaration vector<char*> is a vector of char pointers that's probably intended to carry a collection of C-Style strings. The declaration vector<char> is a vector of individual char.

No i didn't write the function i was given it to write a test program for it. Will try your way thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.