C++ 在C++;
MyFill是一个类,MyFill2是该类中的一个函数C++ 在C++;,c++,class,variables,C++,Class,Variables,MyFill是一个类,MyFill2是该类中的一个函数 在类的公共函数中声明变量(厚度和线型)--> …只需在私有标签(private:)中声明它们,就像在头文件--> 第一个是正确的。但是第二个没有。如果我想选择第二个选项,我需要做什么?正确的代码和一些解释可能会有所帮助。不允许在类的作用域内为变量赋值,只能在函数内部或全局作用域内执行此操作 class MyFill { public: MyFill(Mat img1, Point center1); void MyFill2 (Mat im
在类的公共函数中声明变量(厚度和线型)--> …只需在私有标签(private:)中声明它们,就像在头文件-->
第一个是正确的。但是第二个没有。如果我想选择第二个选项,我需要做什么?正确的代码和一些解释可能会有所帮助。不允许在类的作用域内为变量赋值,只能在函数内部或全局作用域内执行此操作
class MyFill {
public:
MyFill(Mat img1, Point center1);
void MyFill2 (Mat img, Point center);
private:
int thickness;
int lineType;
};
您的标题需要更改为上述内容。然后,您可以在任何喜欢的函数中设置值,最好是像这样设置构造函数:
MyFill::MyFill(Mat img1, Point center1)
{
thickness = -1;
lineType = 8;
}
编辑-针对评论中的问题:
函数参数中变量名的标识符不需要在声明和定义之间匹配,只需要匹配类型及其顺序。它使它更清楚,但它不是必需的
功能原型实际上仅被视为以下内容:
void MyFill2(Mat, Point);
当您给它一个定义时,即标识符的分配真正重要时:
void MyFill2(Mat m, Point p)
{
//....
}
在类定义中声明成员变量,然后在构造函数中使用初始值设定项列表初始化成员变量:
class MyFill {
public:
MyFill(Mat img1, Point center1);
void MyFill2 (Mat img, Point center);
private:
// Just declare the member variables
int thickness;
int lineType;
};
// ...
MyFill::MyFill(Mat img1, Point center1)
: thickness(-1), lineType(8) // Initializer list to initialize the member variables
{
MyFill2(img1, center1);
}
不能在类声明中赋值。
您应该在类的构造函数中执行此操作:
class MyFill {
public:
MyFill(Mat img1, Point center1);
private:
int thickness ;
int lineType;
};
MyFill::MyFill(Mat img1, Point center1) : thickness(-1), lineType(8) {
// ...
}
在前一种情况下,您声明的局部变量仅在函数范围内有效。在外面,你不能使用它们 在后者中,您为类的作用域声明了它们,因此可以在该类的任何函数中引用它们 请注意,只有在使用符合C++11标准的编译器时,您的初始化风格才起作用,使用C++03时,您需要在构造函数中初始化它们:
MyFill::MyFill(Mat img1, Point center1)
: thickness(-1), lineType(8)
{ /* ... */ }
并且只声明它们为
private:
int thickness;
int lineType;
如果在该函数中只需要这些变量,请使用局部变量。在类的公共函数中声明变量(厚度和线型)有什么区别 厚度和线型在函数范围内定义,称为MyFill2函数的局部变量
void MyFill::MyFill2(Mat img, Point center)
{
int thickness = -1; // thickness is a local variable in MyFill2,
// it's destroyed when MyFill2 function goes out of scope
// thickness is not accessable in any other member function of MyFill
// or object.
int lineType = 8; // same here
}
通过将厚度和线型放置在类范围中,它们是类MyFill的成员,并且在所有MyFill对象中都可用
class MyFill {
private:
int thickness = -1; // this is a C++11 form of initialize class member.
// In C++03, you need to initialize them in constructor
// thickness is a member of MyFill, it will exist in all life of MyFill object.
int lineType = 8;
};
在C++03中,可以在成员初始值设定项列表中初始化类成员
MyFill::MyFill(Mat img1, Point center1)
: thickness(0), lineType(0) // Initializer list to initialize the member variables
{
}
希望这能回答您的问题。c++11允许初始化类声明中的非常量和非静态成员,您可以参考本说明中的讨论,因为c++11可以在类声明中初始化类成员。感谢umläute@ridctg很有帮助,但请注意stackoverflow策略不鼓励POST。因为C++11在类声明中初始化也是可能的。初始值设定项列表。。。我想这就是我需要使用的。谢谢@Joachim@soon:我正在使用VS2010。这就是我出错的原因吗?@ridctg,也许吧。我不使用MSVC,所以,我无法告诉您它支持哪些C++11功能。@umläute:我遇到一个错误:在类中只能初始化静态常量整型数据成员谢谢。我想这就是答案。“我现在就去试试。”里奇特:没问题,很高兴它有帮助。比尔兹上面说的话不矛盾吗?“通过将厚度和线型放置在类作用域中,它们是类MyFill的成员,并且在所有MyFill对象中都可用。”@ridctg构造函数中的上述变量仍在类作用域中。他们刚刚被初始化,因此:。它们不是本地范围。很抱歉,我不明白这个问题:(Thnx.我还需要一个信息。我在这里得到的每个答案都使用了“MyFill(Mat img1,Point center 1);void MyFill2(Mat img,Point center);”但我使用的是“MyFill(Mat img1,Point center 1);void MyFill2(Mat img,Point center);”它仍然有效。但我想知道哪一个是正确的,以及为什么。@ridctg一个是构造函数,另一个只是一个具有相同参数的函数。它们都是正确的,并且在您的类中使用它们绝对没有错。@ridctg我想我理解您现在所说的。函数参数中变量名的标识符ters不需要在声明和定义之间进行匹配,只有类型和它们的顺序需要匹配。谢谢…这个答案很常见,我将尝试一下
class MyFill {
private:
int thickness = -1; // this is a C++11 form of initialize class member.
// In C++03, you need to initialize them in constructor
// thickness is a member of MyFill, it will exist in all life of MyFill object.
int lineType = 8;
};
MyFill::MyFill(Mat img1, Point center1)
: thickness(0), lineType(0) // Initializer list to initialize the member variables
{
}