default Constructer问题(默认头文件没有,构造函数,我不是可以不写的么?)

cycchina 2009-05-15 01:56:07
我的头文件,没有写 默认的构造函数(没有变量的那个) 在同名.cpp中,我是否也可以不去写呢?

如果是,我下面的代码,编译的时候报错。(所没有定义 default Constructer)

提示的错误:concordance.cpp(12) : error C2512: 'WordStream' : no appropriate default constructor available

头文件:a.h

#pragma once

#include <fstream>
#include <string>

#include "wordstream.h"
#include "wordtable.h"

class Concordance {
WordStream wordStream; ///< The wordStream from which the concordance is built
WordTable table; ///< The table of words and their occurrence lists

public:

Concordance(std::ifstream& in);

void print(int wordIndent, int lineLength);

void printIndex();

};


实现文件a.c


#include "concordance.h"

//using namespace std;
Concordance::Concordance(std::ifstream& in)
{
in.close();
}

void Concordance::print(int wordIndent, int lineLength)
{
}



void Concordance::printIndex()
{
}


提示的错误:concordance.cpp(12) : error C2512: 'WordStream' : no appropriate default constructor available

请问错误原因。。谢谢
如果能给出修改意见更好了。
...全文
172 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aime99 2009-05-15
  • 打赏
  • 举报
回复
Concordance 中有WordStream类型对象,
而Concordance 的构造函数列表里没有对WordStream进行构造,
所以编译器就试着产生默认构造函数
又因为你写了WordStream的带有参数的构造函数,编译器不会生成默认构造函数
所以就出错了
maosher 2009-05-15
  • 打赏
  • 举报
回复
WordStream wordStream; 是啥?
这个类找不到默认构造函数
nuoshueihe 2009-05-15
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
class A
{
public:
protected:
private:
int a;
int b;
};
class B
{
public:
B(){}
B(int a,int b)
{
this->a=a;
this->b=b;
}
protected:
private:
int a;
int b;
};
class C
{
public:
C(int a=0,int b=0)//这样写相当于既写了系统提供的默认构造函数,又写了自己的构造函数
{
this->a=a;
this->b=b;
}
protected:
private:
int a;
int b;
};
void main()
{
A a1;
B b1;
B b2(2,3);
C c1;
C c2(2,3);//请主意3者的区别

}
chenzhp 2009-05-15
  • 打赏
  • 举报
回复
同上
nuoshueihe 2009-05-15
  • 打赏
  • 举报
回复
不行
当类中没有任何构造函数时候(包括构造函数的重载函数)时,系统会提供默认的构造函数
当你已经显示写出一个构造函数(或构造函数的重载函数)时,系统就不会提供默认的构造函数
lori227 2009-05-15
  • 打赏
  • 举报
回复
你的 WordStream 类肯定有其他的带有参数的构造函数,而没有提供不带参数的构造函数!~~ 这样编译器就不再会自动提供默认的构造了~
光宇广贞 2009-05-15
  • 打赏
  • 举报
回复
WordStream wordStream; ///< The wordStream from which the concordance is built
WordTable table; ///< The table of words and their occurrence lists

改为

WordStream* wordStream;
WordTable* table;

还有,如果你在类定义中没有写任何构造函数,那么编译期会生成默认无参数的构造函数……

如果你在类定义中写了任何构造函数,则编译期不会添加默认构造函数了。如果找不到匹配的构造函数模板,就会报错。

crst_zh 2009-05-15
  • 打赏
  • 举报
回复
因为你提供了一个不是默认的构造函数,所以就不会为你生成默认的了,编译器认为你觉得它做的不好,那就不为了做了。


可是你在cpp中肯定写了这样的代码:
Concordance xxx;
这就是问题的根源,
你只能这样写:

Concordance xxx(参数

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧