I have a vector vector<string> headers; and for each header I want to add a new series to a chart control.
But my code doesn't work:
for (int i = 0; i < headers.size(); i++){
DataVisual_V2::MainForm::chart_data->Series->Add(headers[i]);
}
It gives the error:
IntelliSense: function "System::Windows::Forms::DataVisualization::Charting::SeriesCollection::Add" cannot be called with the given argument list
argument types are: (std::string)
object type is: System::Windows::Forms::DataVisualization::Charting::SeriesCollection
Any idea what could be the problem?
thanks
Benjamin
This function expects a parameter of System::String type, not std::string. System::String has a constructor that takes char*, so you can use
DataVisual_V2::MainForm::chart_data->Series->Add(String(headers[i].c_str()));
Related
I have a function like this:
void getSprite(string *spriteLines[SPRITE_YSIZE]);
And then I have a calling to the function like this:
int main() {
string *spriteLines[SPRITE_YSIZE];
getSprite(spriteLines);
By here, everything OK. But I decided to declare spriteLines as a string instead of a pointer so I changed the code to this:
int main() {
string spriteLines[SPRITE_YSIZE];
getSprite(&spriteLines);
And an error shows up:
error: cannot convert ‘std::__cxx11::string (*)[5] {aka std::__cxx11::basic_string<char> (*)[5]}’ to ‘std::__cxx11::string** {aka std::__cxx11::basic_string<char>**}’ for argument ‘1’ to ‘void getSprite(std::__cxx11::string**)’ getSprite(&spriteLines);
Do someone of you knows why? I can't understand this.
Extra data: I'm using Eclipse Oxygen v1 and GNU G++.
In the first example, you declared an array of string pointers. In the second example you declared an array of strings. Both arrays are passed your function as a pointer. Your error occurs because the are different "types".
In the second example you would need to change your getSprite function to:
void getSprite(string spriteLines[SPRITE_YSIZE])
I am trying to print the elements of a set containing strings on graphics.h console using outtext() function,but i get this error:
cannot convert 'std::string {aka std::basic_string}' to 'char*' for argument '1' to 'void outtext(char*)'|
this the piece of code that gives error:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j]);
}
}
the template for the outtext function in the graphics.h header is like this:
void outtext(char *textstring);
i have used c_str() like this:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j].c_str());
}
}
but this time it gives this error:
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]|
You can try this one as well:
char *cstr = new char[21]; // just in case string length is maxed at 20, leave 1 character for '\0'
for (int i = 0; i<20; i++) {
for (int j = 0; j<20; j++) {
strcpy_s(cstr, str[i][j].length() + 1, str[i][j].c_str());
outtext(cstr);
}
}
delete[] cstr;
Just added a char* string to temporarily hold the converted std::string value. The tricky part is that char* strings normally have the terminating character \0 which std::string don't have, so you have to add 1 more character to the size of each "row" of str.
I take it this question is about the 30 years old BGI graphics library and Borland C++. The root of the problem is that this library was poorly written, as it didn't implement const correctness.
The Turbo C++ compiler did not follow anything remotely close to any C++ standard, so you are mostly out of luck. If you had a proper C++ compiler you could use const_cast, but I very much doubt this is available to you.
The only solution left is the dirty, bad way:
outtext((char*)str[i][j].c_str()); // bad practice
You should never cast away const like this in neither C nor C++.
If you can change the prototype of the output function then it is better to change void outtext(char *textstring); to void outtext(const char *textstring); because there is no need for the output function to modifiy the string. Otherwise you could use const_cast before passing to the function like outtext(const_cast<char*>(str[i][j].c_str())) or copy the string to another char* and passed the copied value.
This is in reference to the following answer by Synxis.
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715#18715
Suppose, I have to print all substrings of the string "cbaa". To do this, I have to invoke the method like this:
findAllSubstrings2("cbaa");
If I take a string from user, and do the following:
string s;
cin>>s;
findAllSubstrings2(s);
it gives the following error:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
Why does this happen?
As the error message says the parameter of function findAllSubstrings2 is declared as having type const char * while you are trying to pass an argument of type std::string
string s;
//...
findAllSubstrings2(s);
You should use member function c_str or data (starting from C++ 11) of class std::string. For example
findAllSubstrings2(s.c_str());
you using string, in function is char try to use char[] s;
use c_str() method in string class when passing the argument
string s;
cin>>s;
findAllSubstrings2(s.c_str());
You probably should change the type of the parameter of the function. Somethink like:
void findAllSubstrings2(string s){
//... function implementation...
}
I'm trying to use a separate function to read a few data values in from a file; I'm getting two errors (I haven't used c++ in a long time...):
double * FREAD(std::string fpath){
std::string line;
double * params = new double[14];
std::ifstream parameters (fpath);
if (parameters.is_open())
{
for (int b = 0; b < 14; b++) {
parameters >> line >> params[b];
}
}
parameters.close();
return params;
}
throws
error C2556: 'double *FREAD(std::string)' : overloaded function differs only by return type from 'double FREAD(std::string)'
and
error C2040: 'FREAD' : 'double *(std::string)' differs in levels of indirection from 'double (std::string)'
The second issue is thrown from the line where I call the function in main.
double * param = FREAD(parampath);
error C2440: 'initializing' : cannot convert from 'double' to 'double *'
If I don't define param as a value pointed at by a double, I get the same type mismatch error in reverse...
My understanding is that I'm supposed to return a pointer which is directed at the first value of the array my subfunction has created and use that to work with the data. But I can't seem to pull this value when I call the function in main.
The simplest and fool-proof way to do it would be to return a container, for example
#include <array>
std::array<double,14> FREAD(const std::string& fpath)
{
std::string line;
std::array<double, 14> params;
// .... fill params
return params;
}
Concerning the overload error, you cannot overload a function by return type. It looks like the compiler is seeing these two declarations:
double * FREAD(std::string fpath);
double FREAD(std::string fpath);
Given the above suggestion, you should remove both of them anyway.
Your error C2556 is because you apparently have another FREAD() function that returns something else. The error is telling you that you can't overload functions based only on the return type.
Going from the messages, it appears to me that you have two functions:
double * FREAD(std::string fpath)
double FREAD(std::string fpath)
You only posted the first one.
C++ only allows you have two functions with the same name if they take different arguments (or const-ness for member functions). You should either give your functions different names, or pass in a token argument that the compiler can use to tell which one you want.
string input;
string code[4];
if (code.find(o) == input.find(o))
{
}
For this line it gives me the error: request for member 'find' in 'code', which is of non-class type 'std::string [4]'
Both string input and string code have string values in them.
The compiler is telling you that code is an array of strings, so you need something like
code[someIndex].find(o) == ....
The error says all , code is an array of std::string
compare using
code[i].find(o)
i = looping index
Just read the error:
error: request for member 'find' in 'code', which is of non-class type 'std::string [4]
It is telling you that:
code is of type std::string [4] (which is an array of 4 std::string objects)
it couldn't find the member function find you are requesting
Just select the correct index for the string you want to call find on or do a loop:
for (int i = 0; i < 4; i++)
if (code[i].find(o) == input.find(o))
// ...
Note that, if you can, you should try to avoid C-style arrays and use std::array (since C++11) or std::vector instead.