简体   繁体   English

const char *变量作为函数的参数

[英]const char* variable as parameter to function

I have this constructor implemented in a C++ library(by someone else, not by me): 我在C ++库中实现了此构造函数(由其他人而不是由我):

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). 此函数有用(),计算要作为参数传输到构造函数Variables(const char * s)的int。

So I am converting the int to a string and then to a const char* data type. 所以我将int转换为字符串,然后转换为const char *数据类型。 And them I am transmitted it as a parameter to the Variables constructor, but I get an error message. 然后我将它们作为参数传递给Variables构造函数,但出现错误消息。

Depending on the integers in the variable loopOE, I am constructing a string "xi", where i is the ith integer from the vector loopOE. 根据变量loopOE中的整数,我正在构造一个字符串“ xi”,其中i是向量loopOE中的第i个整数。 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). 例如,loops [0] = 2,然后我们创建字符串“ x2”,然后将其转换为const char * ,然后传输给构造函数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 QSweepComplete.cpp:在成员函数'void QSweepComplete :: prealexMatrix()'中:QSweepComplete.cpp:975:错误:未在此范围内声明'varPolyConverted'使得:*** [.obj / QSweepComplete.o]错误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 最好的祝福,麦达利娜

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. varPolyConverted变量仅在循环内部可见,并且您尝试在循环之后使用它-不允许这样做,因为该变量当时不在范围内。 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. 如果选择前者要小心-您有很多STL对象,并且想要一个指向其中一个缓冲区的指针-该对象的作用域必须至少与其指向其缓冲区的指针相同,否则会遇到不确定的行为。

The problem is that you're declaring varPolyConverted inside of the for loop. 问题是您在for循环中声明了varPolyConverted。 Add

const char* varPolyConverted;

before the for loop and then just do: 在for循环之前,然后执行以下操作:

 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. 将函数调用与声明变量的范围放在同一范围内会更好。

变量varPolyConverted的作用域在for循环内,无法在外部访问。

You get the error because const char* varPolyConverted is declared inside a block. 您会收到错误消息,因为const char * varPolyConverted在块内声明。 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. 您可以在for循环之前定义它,然后在for循环之后也可以看到它。

在第二个循环之前声明varPolyConverted

const char* varPolyConverted

is defined in your for loop. 在for循环中定义。 So after the for loop it is no longer a valid declaration. 因此,在for循环之后,它不再是有效的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM