Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Class Help

Name: Anonymous 2009-03-30 22:42

Halp /prog/
I'm supposed to make a program that asks how many integers to sums up, then using a for loop, allows the user to input the integers. I posted earlier about it, but now I'm trying to use a class, and failing horribly. This is my first programming language class, so if you could provide as many tips as possible that would be appreciated (other than the obvious 'GTFO' or 'change your major lol randumb xD')


//////////////////////////////////////////////////////////////////
//    Lab 5 - For loop Sum
//    Header.h
//////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

class Sum
{
    public:
        Sum();    //constuctor
        void calcSum(int);    //adds to the total
        int setSum();    //displays total
    private:
        int m_total;
}

Sum::Sum ()
{
    calcSum(m_total);
    setSum();
}

void Sum::calcSum (int a_temp)
{
    int m_total = m_total + a_temp;
}

int Sum::setSum (void)
{
    return m_total;
}


//////////////////////////////////////////////////////////////////
//    Lab 5 - For loop Sum
//    Test.cpp
//////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

#include "Header.h"

int main()
{
    int x = 0;    // x = the number of times the for loop will run
    int i = 0;
    int num = 0;
    Sum total;

    cout << "Number of integers to sum: ";
    cin >> x;

    for(i = 0; i >= x; i++)
    {
        cout << "Enter number " << (i+1) << ": ";
        cin >> num;
        cout << endl;
        total.calcSum(num);
    }

    cout << "\n\nTotal sum: " << total.setSum() << endl;

    return 0;
}

Name: Anonymous 2009-03-30 22:43

I keep getting 2 errors:

error C2533: 'Sum::{ctor}' : constructors not allowed a return type

error C2264: 'Sum::Sum' : error in function definition or declaration; function not called

Name: Anonymous 2009-03-30 22:53

I see you have assumed that C++ has a sane syntax. Don't do that. You're missing a semicolon.

Name: Anonymous 2009-03-30 22:54

put

Sum::Sum ()
{
    calcSum(m_total);
    setSum();
}

void Sum::calcSum (int a_temp)
{
    int m_total = m_total + a_temp;
}

int Sum::setSum (void)
{
    return m_total;
}

in a .cpp
Google a basic tutorial. Otherwise, your a standard prog user.

Name: Anonymous 2009-03-30 23:01

>>4
Who cares? You have to put the implementation of templated classes in header files anyway. Might as well throw whatever the fuck you want into the damn things.

Anyone using Sepples has far bigger problems than slow compile times.

Name: Anonymous 2009-03-30 23:09

>>5
The linker cares.
And in VS, that can cause a bitchload of errors.

Name: Anonymous 2009-03-31 1:00

>>6
And that bitchload of errors is because C++ is a stupid and broken language. It's not regular, so parsing is difficult at best, especially trying to keep track of error messages.

Name: Anonymous 2009-03-31 5:32

class Sum {
    ...
}
;

You forgot the semicolon after the class declaration.

Also, in Sum::calcSum you are declaring a local variable m_total, instead of assigning to the member variable Sum::m_total.

Name: Anonymous 2009-03-31 5:37

WTF are you using a class for?

Name: Anonymous 2009-03-31 5:56

>>8
};

Name: Anonymous 2009-03-31 6:03

>>9
Sepples, like Java, is a class-oriented language. If you want to sum a list, you need to create a class. If you want to add two numbers, you need to create a class. If you want to output "Hello, World!", you need to create a class. That's just the way the language works.

Name: Anonymous 2009-03-31 6:20

>>11
I think you need to reread K&R.

Name: Anonymous 2009-03-31 9:02

>>12
I think you need to reread YHBT.

Name: Anonymous 2009-03-31 9:34

>>6
The linker doesn't care if the header is only included in a single compilation unit (or if the functions are defined inline). The former is true in this case so it doesn't make that big of a difference.

But let's dispense with these trivial matters and get back to writing code with the fictional language Haskell.

Name: Anonymous 2009-03-31 10:38

>>13
possibly YHBMT

Name: Anonymous 2009-03-31 14:43

OP here, I lolled at all the responses.
Thanks for all the help.

And I'm using classes because it's part of the assignment (create a class Sum).

And we have to combine the Header and the Definitions due to the professors preference.
Is there any significant difference in having them combined/not?
Or should I just pass the class and never use C++ again?

Name: Anonymous 2009-03-31 14:44

>>16
Drop the class and watch the MIT lectures.

If you don't, never post here again.

Name: Anonymous 2009-03-31 14:47

>>16
You should transfer to a real middle school ``college'' and not deal with this bullshit in the first place. Of course, since you came to 4chan for help with your shitty homework problems, you aren't the kind of person who would ever graduate anyway.

Name: dude 2009-03-31 15:37

>>1
In addition to the missing semi colon, you should set m_total to 0 in the constructor(no need for prefixes on your variables here either for that matter) also the two functions you call in the constructor have no effect.  Also yes, there is many sucky things about C++, it will help you learn about low level languages though, and it will make it easy to see how everything fits together when you learn about assembly and computer architecture.  IMO colleges should teach vanilla C instead of C++. (or at least if they use C++, stay away from classes, templates etc. and save that stuff for the java or C# class).

Name: Anonymous 2009-03-31 15:46

>>19
you should set m_total to 0 in the constructor
No, you should set it to 0 in the initializer list.

>>16
And we have to combine the Header and the Definitions due to the professors preference.
Your professor is an idiot if he's suggesting that. It works fine for this example, but is a terrible practice in The Real Worldtm.

Is there any significant difference in having them combined/not?
Yes. Assume you wanted to use your useless Sum class in another class's implementation (ie, within another *.cpp file). You'd include the sum.h header in the second implementation file, naturally.

And the linker would shit down your throat for breakfast.

The reason is that #include (and headers in general) are simply preprocessing sugar. The contents of the header files are just dumped verbatim into wherever. So if you put definitions into a header file, then include that header file in multiple compilation units, each compilation unit will have a copy of the same definition. The linker won't know which one to use when it puts everything together, and will barf.

So please, for the sake of the linker's stomache keep all your un-inlined function definitions out of the header files. Thanks.

Name: dude 2009-03-31 17:53

>>20
An "initializer list" does the same thing, don't be pedantic.

Name: Anonymous 2009-03-31 19:29

GTFO

Name: Anonymous 2009-03-31 20:23

>>21
0/10

Name: Anonymous 2009-03-31 21:11

>>21
The initializer list is part of the constructor, be pedantic.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List