简体   繁体   English

我丢失了一些东西,并且尝试调试了一段时间(对c ++来说是新的)

[英]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.

相关问题 C ++:虽然循环不会以NULL结尾,但是我错过了什么? - C++: While loop won't terminate as NULL, am I missing something? 这是Visual C ++ 2010中的错误,还是我错过了什么? - Is this a bug in Visual C++ 2010, or am I missing something? 我错过了什么吗? - Am I missing something? c++ 段错误和指针混乱(我觉得我错过了一些非常明显的东西) - c++ segfault and pointers confusion (I have the feeling I'm missing something terribly obvious) 同样的逻辑适用于c ++但不能在python中最大化堆栈,我的代码中缺少一些东西 - Same logic is working for c++ but not in python for maximum in stack,is there something that i am missing in my code GCC -E -MD有错误还是我错过了什么? - does GCC -E -MD have a bug or am I missing something? 这是一个错字还是我错过了什么? - is this a typo or am i missing something? C ++多态性:我缺少什么? - C++ polymorphism: what am I missing? 我对 C++ 中的 STL 相当陌生,我尝试使用向量进行堆。 没有得到想要的 output - I am fairly new to STLs in C++ and i tried making a heap using vectors. Didnt get the desired output 我做了什么多余的事情,或者我是一个 C++ 新手,在这里错过了什么吗? - Is there anything redundant that I have done or am I, a C++ newbie, missing out anything here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM