令人困惑的错误 [英] confusing errors

查看:58
本文介绍了令人困惑的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>

#include< string>

使用命名空间std;


class A

{

public:

int x;

int y;

A(int y_ ){y = y_;}

};


int main()

{

aobj;

返回0;

}


当我定义一个对象时,一些数据成员无法初始化而且没有

适当的默认构造函数可用。所以,有一个错误。


但是,即使我明确添加了默认构造函数,下面的代码中还有

仍有错误。


#include< iostream>

#include< string>

使用命名空间std;


级A

{

public:

int x;

int y;

A();

A(int y _){y = y_;}

};


int main( )

{

A aobj;

返回0;

}


为什么?

解决方案

heng写道:


#包括< iostream>

#include< string>

使用命名空间std;


class A

{

public:

int x;

int y;

A(int y _){y = y_;}



首选分配初始化。请参阅常见问题解答。


};


int main()

{

Aobj;

返回0;

}


当我定义一个对象时,一些数据成员不能初始化和

没有合适的默认构造函数可用。所以,有一个

错误。


但是,即使我明确地添加了默认构造函数,也有

仍然存在错误在以下代码中。


#include< iostream>

#include< string>

using namespace std;


A级

{

公开:

int x;

int y;

A();

A(int y _){y = y_;}

};

int main()

{

A aobj;

返回0;

}


为什么?



*什么*错误?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问




heng写道:


#include< iostream>

#include< string> ;

使用命名空间std;


A级

{

public:

int x;

int y;

A(int y _){y = y_;}

};


int main()

{

A aobj;

返回0;

}


当我定义一个对象时,一些数据成员无法初始化,并且没有

适当的默认构造函数可用。所以,有一个错误。



是的,该代码将失败,因为没有默认构造函数

您试图使用一个。


>

但是,即使我明确添加默认构造函数,仍有

仍有错误以下代码。


#include< iostream>

#include< string>

using namespace std;


A级
{

public:

int x;

int y ;

A();

A(int y _){y = y_;}

};


int main()

{

A aobj;

返回0;

}


为什么?



这个将无法链接,因为你的

默认构造函数没有定义。


>


这个将无法链接,因为您的

默认构造函数没有定义。



我是否需要定义我的默认构造函数?我认为默认的

构造函数总是如下:


class Foo

{

public :

Foo(); //默认构造函数

}


#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

When I define an object, some data members cannot be initialized and no
appropriate default constructor is available. So, there is an error.

However, even if I add the default constructor explicitly, there are
still errors in the following code.

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A();
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

Why?

解决方案

heng wrote:

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A(int y_){y=y_;}

Prefer initialisation over assignment. See FAQ.

};

int main()
{
A aobj;
return 0;
}

When I define an object, some data members cannot be initialized and
no appropriate default constructor is available. So, there is an
error.

However, even if I add the default constructor explicitly, there are
still errors in the following code.

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A();
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

Why?

*What* errors?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



heng wrote:

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

When I define an object, some data members cannot be initialized and no
appropriate default constructor is available. So, there is an error.

Yes, that code will fail because there is no default constructor yet
you are attempting to use one.

>
However, even if I add the default constructor explicitly, there are
still errors in the following code.

#include<iostream>
#include<string>
using namespace std;

class A
{
public:
int x;
int y;
A();
A(int y_){y=y_;}
};

int main()
{
A aobj;
return 0;
}

Why?

This one will fail to link because there is no definition for your
default constructor.


>

This one will fail to link because there is no definition for your
default constructor.

Do I need to define my default constructor? I think the default
constructor is always as follows:

class Foo
{
public:
Foo(); //default constructor
}


这篇关于令人困惑的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆