473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Vector with char type

16
I'm currently having a confusion with storing characters into a vector. The problem is described as follows:

I've a string array which stores, let's say the names of the vegetables.

string vegeName[20];

and this array is stored fully with all the names. Then I need to store one of the element in this string array into a vector. The project itself is asking us to do so, so I've no choice. So this is what I did.

vector <char> vegeVec;
vegeVec.push_back(vegeName[0]);

This gives me a compilation error saying no matching function for bla bla bla bla..

Can I know where is my mistake and what should I do to correct it? Thank you
Oct 3 '07 #1
5 1753
Savage
1,764 Expert 1GB
I'm currently having a confusion with storing characters into a vector. The problem is described as follows:

I've a string array which stores, let's say the names of the vegetables.

string vegeName[20];

and this array is stored fully with all the names. Then I need to store one of the element in this string array into a vector. The project itself is asking us to do so, so I've no choice. So this is what I did.

vector <char> vegeVec;
vegeVec.push_back(vegeName[0]);

This gives me a compilation error saying no matching function for bla bla bla bla..

Can I know where is my mistake and what should I do to correct it? Thank you
This is normal.You have array of chars,so each element is just that a single char,and you are trying to add a whole string to it,think about this..


Savage
Oct 3 '07 #2
RedSon
5,000 Expert 4TB
I'm currently having a confusion with storing characters into a vector. The problem is described as follows:

I've a string array which stores, let's say the names of the vegetables.

string vegeName[20];

and this array is stored fully with all the names. Then I need to store one of the element in this string array into a vector. The project itself is asking us to do so, so I've no choice. So this is what I did.

vector <char> vegeVec;
vegeVec.push_back(vegeName[0]);

This gives me a compilation error saying no matching function for bla bla bla bla..

Can I know where is my mistake and what should I do to correct it? Thank you
ok take a close look at this:
Expand|Select|Wrap|Line Numbers
  1. string vegName[20];
  2. vector <char> vegeVec;
  3. vegeVec.push_back(vegeName[0]);
What could be the problem here? You vector is a vector of what data type? Your VegName array is an array of what data type?
Oct 3 '07 #3
RedSon
5,000 Expert 4TB
This is normal.You have array of chars,so each element is just that a single char,and you are trying to add a whole string to it,think about this..

Savage
Savage, with the quick fingers!
Oct 3 '07 #4
Ganon11
3,652 Expert 2GB
Well, vegeName[0] is a string object, right? The brackets [] 'dereference' (for lack of a better term) the vector to get the first string stored in it.

Now you try to push it onto vegeVec, which is expecting a char. Uhh...a string isn't a char!

I expect you want vegeVec to hold char*, not char. A char* could hold a CString, which is starting to get closer to a string object. But, string != char*. So you need a little extra help: check out the c_str() function for strings.
Oct 3 '07 #5
Savage
1,764 Expert 1GB
Savage, with the quick fingers!
Got ya this time..

haha,another one down.

Too late gannon! :P

Savage
Oct 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
16
by: Vince | last post by:
Hi, I have replaced my BYTE* by a vector<BYTE> and before I used to do : void CCardRecord::GetRecData(int nOffset, int nDataSize, CString& csValue) { BYTE *pTmp = NULL; pTmp = new BYTE;...
8
by: Simon Elliott | last post by:
#include <vector> #include <iostream> int main (int argc, char *argv) { std::vector<int> vi; vi.push_back(1); vi.push_back(2); vi.push_back(3); int* pi = vi.begin(); std::cout << "result:"...
5
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
9
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
14
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
5
by: mosfet | last post by:
Hi, In one my class I would like to give access to one of my buffer so I have declared something like this : vector<char>& RespData = pWebResp->get_RawBuffer(); The problem is caller can...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.