[英]I am missing something and I have tried to debug this for a while (im new to c++)
I do not know why the proggram isnt working. 我不知道为什么程序不起作用。 I am just testing out a program to see how classes work.
我只是测试一个程序,以查看类如何工作。 what does static do?(I looked up what it does but could I get some explaining).
静态功能是什么?(我查看了它的功能,但可以解释一下)。 And how could I improve the structure of the code?
以及如何改善代码的结构?
Source.cpp Source.cpp
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include "Source1.cpp"
using namespace std;
main() {
float sum1, sum2;
cout << "Sum1 \n";
cin >> sum1;
cout << "Sum2 \n";
cin >> sum2;
cout << how_to_add::addition(sum1,sum2) << endl;
return 0;
}
Source1.cpp Source1.cpp
class how_to_add {
float sum1, sum2, added;
public:
static float addition(float sum1, float sum2) {
float added = sum1 + sum2;
return added;
}
};
This is the error I'm having: 这是我遇到的错误:
1>------ Build started: Project: Project1, Configuration: Debug x64 ------
1>Source.cpp
1>c:\users\lisa\documents\visual studio 2017\projects\project1\project1\source.cpp(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
In C, the way you declared main
, that is without explicitly stating the return type, it would not give you an error, but your C++ compiler doesn't like it (and it's probably compiling in a restricted mode since that should only give you an warning), apparently. 在C语言中,您声明
main
的方式(即没有明确说明返回类型)不会给您带来错误,但是您的C ++编译器不喜欢它(并且它可能在受限模式下进行编译,因为那只会给您警告)。 So I would suggest you to change: 因此,我建议您更改:
main() {
...
to 至
int main() {
...
return 0;
Why you name the source file as ".cpp"? 为什么将源文件命名为“ .cpp”? Use ".h" instead of ".cpp".
使用“ .h”而不是“ .cpp”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.