【发布时间】:2020-12-11 18:32:52
【问题描述】:
main.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
int main(){
Shreeman object;
}
shreeman.h
#ifndef SHREEMAN_H
#define SHREEMAN_H
class Shreeman
{
public:
shreeman();
};
#endif // SHREEMAN_H
shreeman.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
Shreeman::shreeman()
{
cout<<"Hello Hello"<<endl;
}
为什么这段代码不输出“Hello Hello”?我在 main.cpp 文件中创建了一个对象,但控制台中没有打印任何内容。
【问题讨论】:
-
你发了两次
shreeman.h,没有发shreeman.cpp。 -
shreeman不是Shreeman的构造函数。 -
你拼错了构造函数的名字——它有一个小写的
s而不是一个大写的S(我希望你的编译器至少给出一个警告)? -
@Shreeman24 你的类定义中的
shreeman();是如何编译的? -
@Shreeman24 我们无法为您提供帮助,因为您发布的代码是您认为的代码,而不是实际代码,而且区别很重要。请制作一个[Minimal Reproducible Example](如何创建一个Minimal, Reproducible Example)
标签: c++ class constructor