error C2512: “HelloWorld”: 没有合适的默认构造函数可用
c++ newbie error C2512: no appropriate default constructor available
解决办法
在类中添加一个空的构造函数。问题解决
实例
错误程序
#include <iostream>
#include "helloworld.h"
int main(void)
{
HelloWorld hello;
hello.say();
while(1){}
return 0;
}
main.cpphelloworld.h
#ifndef __HELLOWORLD_H_
#define __HELLOWORLD_H_
class HelloWorld{
public:
HelloWorld(int a):a(a){}
void say(){
std::cout << "Hello World!" << std::endl;
}
int a;
};
#endif
编译失败:
1> main.cpp : error C2512: “HelloWorld”: 没有合适的默认构造函数可用
修改后正确的程序
修改helloworld.h
#ifndef __HELLOWORLD_H_
#define __HELLOWORLD_H_
class HelloWorld{
public:
HelloWorld(int a):a(a){}
HelloWorld(){}
void say(){
std::cout << "Hello World!" << std::endl;
}
int a;
};
#endif
编译成功:
1>生成成功。
原文:https://blog.csdn.net/github_35160620/article/details/51718157