[英]C++ program error on passing arguments to the parameterized constructor by pointer object?
我有一个名为sample
的 class 。
class sample
{
private:
int Age;
public:
sample() : Age() {}
sample(int age)
{
this->Age = age;
}
};
在主要
int main()
{
sample* s = new sample();
s(21);
return 0;
}
我想通过指针 object 将值传递给参数化构造函数,但它给出了这个错误
明显调用括号前的表达式必须具有(指向-)function 类型
您已经在执行new sample()
时构建了 object,因此您可以在此处传递您的构造函数值。 您不能两次构造 object。
像这样做:
sample* s = new sample(21);
或者
sample* s;
s = new sample(21);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.