[英]C++ program error on passing arguments to the parameterized constructor by pointer object?
I have a class called sample
.我有一个名为
sample
的 class 。
class sample
{
private:
int Age;
public:
sample() : Age() {}
sample(int age)
{
this->Age = age;
}
};
In main在主要
int main()
{
sample* s = new sample();
s(21);
return 0;
}
I want to pass values to the parameterized constructor through pointer object but it's giving this error我想通过指针 object 将值传递给参数化构造函数,但它给出了这个错误
expression preceding parentheses of apparent call must have (pointer-to-) function type明显调用括号前的表达式必须具有(指向-)function 类型
You've already constructed the object when did new sample()
, so you can pass your constructor values right there.您已经在执行
new sample()
时构建了 object,因此您可以在此处传递您的构造函数值。 You cannot construct the object twice.您不能两次构造 object。
Do it like this:像这样做:
sample* s = new sample(21);
or或者
sample* s;
s = new sample(21);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.