Note that there are some explanatory texts on larger screens.

plurals
  1. POconst char* variable as parameter to function
    text
    copied!<p>I have this constructor implemented in a C++ library(by someone else, not by me):</p> <pre><code>Variables(const char *s) </code></pre> <p>Which I want to use in my own function, useful(). This functions useful(), computes an int which I want to be transmitted as a parameter to the constructor Variables(const char *s).</p> <p>So I am converting the int to a string and then to a const char* data type. And them I am transmitted it as a parameter to the Variables constructor, but I get an error message.</p> <p>Depending on the integers in the variable loopOE, I am constructing a string "xi", where i is the ith integer from the vector loopOE. For example is loops[0]=2, then we create the string "x2", which is then converted to a <code>const char *</code>, and then transmitted to the constructor Variables(const char* s).</p> <p>My function useful() looks in the following way:</p> <pre><code>void useful(){ vector&lt;int&gt; loopsOE; for (unsigned int i=0;i&lt;6;i++) loopsOE.push_back(i); for (unsigned int i=0;i&lt;loopsOE.size();i++){ //convering int to string std :: ostringstream sstreamComplete; sstreamComplete &lt;&lt; loopsOE[i]; std :: string loopsOEStr=sstreamComplete.str(); //construct the string variable "xi" string varPoly("x"); varPoly.append(loopsOEStr); //converting the string to char* const char* varPolyConverted=varPoly.c_str() } std::vector&lt; polynomial_t &gt; Vec(myEdgesIntersect.size()); Variables V(varPolyConverted); } </code></pre> <p>When I try to compile this function I get the following error message:</p> <blockquote> <p>QSweepComplete.cpp: In member function 'void QSweepComplete::prealexMatrix()': QSweepComplete.cpp:975: error: 'varPolyConverted' was not declared in this scope make: *** [.obj/QSweepComplete.o] Error 1</p> </blockquote> <p>Still I don't understand as if I modify the function with a simple constant in the following way:</p> <pre><code>void usefulModified(){ vector&lt;int&gt; loopsOE; for (unsigned int i=0;i&lt;6;i++) loopsOE.push_back(i); for (unsigned int i=0;i&lt;loopsOE.size();i++){ //convering int to string std :: ostringstream sstreamComplete; sstreamComplete &lt;&lt; loopsOE[i]; std :: string loopsOEStr=sstreamComplete.str(); //construct the string variable "xi" string varPoly("x"); varPoly.append(loopsOEStr); //converting the string to char* const char* varPolyConverted=varPoly.c_str() } std::vector&lt; polynomial_t &gt; Vec(myEdgesIntersect.size()); const char * str="x0"; Variables V(str); } </code></pre> <p>the function compiles and runs without any problems.</p> <p>If anyone has any suggestions, I will strongly appreciate them.</p> <p>Thanks in advance. Best wishes, madalina</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload