-1

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

#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

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 ==========
nbro
  • 15,395
  • 32
  • 113
  • 196
  • 4
    What do you mean by the program isn't working? Are you having an error? If yes, which one? Edit your question to add these details! – nbro Mar 11 '17 at 02:31
  • Regarding `static`, to simplify it in a way that focuses on your example, it lets you call the `addition` method without having to create an instance of the `how_to_add` class first - by using the scope resolution operator `::`. – domsson Mar 11 '17 at 02:38
  • @nbro I made the edit – laquishabonquiquithe3rd Mar 11 '17 at 02:45
  • 1
    Where it says `source.cpp(11)` that means the error is on line 11 of `source.cpp`. The rest of the message tells you exactly what is wrong with that line – M.M Mar 11 '17 at 02:46
  • `main() {` -- See anything wrong with that line? – PaulMcKenzie Mar 11 '17 at 02:46

2 Answers2

2

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. So I would suggest you to change:

main() {
    ...

to

int main() {
    ...
    return 0;
nbro
  • 15,395
  • 32
  • 113
  • 196
0

Why you name the source file as ".cpp"? Use ".h" instead of ".cpp".

Boooooo
  • 157
  • 3
  • 12
  • I thought .h files were made for just declaring a function. I asked about putting this stuff in .h files here: ( http://stackoverflow.com/posts/42685649/revisions ) but they pointed me to this link ( http://stackoverflow.com/questions/9075931/function-declaration-inside-or-outside-the-class ) and it said you put the functions definiton/what its supposed to do in a .cpp file and just declare a function in a .h file... so thats why. – laquishabonquiquithe3rd Mar 11 '17 at 03:06
  • Add "int" before you main function. – Boooooo Mar 11 '17 at 03:13