tags:

views:

157

answers:

6

Hello!

I have this constructor implemented in a C++ library(by someone else, not by me):

Variables(const char *s)

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).

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.

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 const char *, and then transmitted to the constructor Variables(const char* s).

My function useful() looks in the following way:

void useful(){
    vector<int> loopsOE;
    for (unsigned int i=0;i<6;i++)
          loopsOE.push_back(i);

for (unsigned int i=0;i<loopsOE.size();i++){
 //convering int to string
 std :: ostringstream sstreamComplete;
 sstreamComplete << 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< polynomial_t > Vec(myEdgesIntersect.size());
Variables V(varPolyConverted);
}

When I try to compile this function I get the following error message:

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

Still I don't understand as if I modify the function with a simple constant in the following way:

void usefulModified(){
    vector<int> loopsOE;
    for (unsigned int i=0;i<6;i++)
          loopsOE.push_back(i);

for (unsigned int i=0;i<loopsOE.size();i++){
 //convering int to string
 std :: ostringstream sstreamComplete;
 sstreamComplete << 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< polynomial_t > Vec(myEdgesIntersect.size());
    const char * str="x0";
Variables V(str);
}

the function compiles and runs without any problems.

If anyone has any suggestions, I will strongly appreciate them.

Thanks in advance. Best wishes, madalina

+3  A: 

varPolyConverted variable is only visible inside the loop and you try to use it after the loop - this is not allowed since the variable is out of scope at that moment. That's why the compiler refuses to compile the code.

You have to change your code so that you only use the variable within its scope - either declare it out of the loop or call the function inside the loop.

If you choose the former be careful - you have a lot of STL objects and you want a pointer to the buffer of one of them - the object must have at least the same scope as the pointer to its buffer otherwise you run into undefined behaviour.

sharptooth
+2  A: 

The problem is that you're declaring varPolyConverted inside of the for loop. Add

const char* varPolyConverted;

before the for loop and then just do:

 varPolyConverted = varPoly.c_str();

EDIT: this will fix the compile time error, but may leave you with a dangling pointer. Putting the function call in the same scope as where the variable is declared would be better.

AlbertoPL
If he does so he will get a dangling pointer.
sharptooth
in that case assigning 0 to varPolyConverted before exiting the loop should work.
AlbertoPL
Then he will always have zero at the site of function call which is not likely what he wants.
sharptooth
You're right, the function call should probably remain in the same scope
AlbertoPL
A: 

The scope of variable varPolyConverted is inside the for loop and it can not be accessed outside.

Naveen
A: 

You get the error because const char* varPolyConverted is declared inside a block. You are trying to access it outside of that block, it is not visible outside of the block.

You could define it before for loop then it will be visible after the for loop as well.

stefanB
A: 

Declare varPolyConverted before the second loop

gpeche
A: 
const char* varPolyConverted

is defined in your for loop. So after the for loop it is no longer a valid declaration.

Totonga