0

Hello there stackOverflow.
I am a noob when it comes to c++ and today i tried to compile my first program including classes. This ofcourse gave me an error and i don't really know what i did wrong since its my first time compling a class program :P

i tried to google it and it said something about either the DLL.'s are building wrong? (i got it all in debug mode, so it can't be that). So the other option is: i might be freeing the memory wrongly or something like that, can anyone explain where i did wrong?

i tried to compare the code to the tutorial i found. But that didn't help. (the tutorial im referring to is: http://www.youtube.com/watch?v=vz1O9nRyZaY ). The error i'm getting is

void __cdecl _free_base (void * pBlock)
{

    int retval = 0;


    if (pBlock == NULL)
        return;

    RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));

    retval = HeapFree(_crtheap, 0, pBlock);
    if (retval == 0)
    {
        errno = _get_errno_from_oserr(GetLastError());
    }
}  

and my source code / header files are:
main.cpp

#include <iostream>
#include <string>
#include "conio.h"
#include <crtdbg.h>
#include "Tax.h"

using namespace std;

int main()
{

string name;
double tax;
double income;

cout << "Enter your name: ";
cin >> name;
cout << "Enter your annually income: ";
cin >> income;
cout << "Enter your tax rate in pure numbers: ";
cin >> tax;

Tax Customer_1(name, income, tax);

cout << endl << "Customer name: " << Customer_1.getName() << endl <<
    "Income annually: " << Customer_1.getIncome() << endl <<
    "Tax percent(in real numbers): " << Customer_1.getTax() << endl <<
    "Your income after tax, per month is: " << Customer_1.calculateTax() << endl;

cout << endl;

_getch();

return 0;
}

Tax.h:

#include <iostream>
#include <string>

using namespace std;

#ifndef TAX_H
#define TAX_H

class Tax
{
public:

//Default constructor
Tax();

//Overload constructor
Tax(string, double, double);

//destructor
~Tax();

//Accessor functions
string getName() const;

double getIncome() const;

double getTax() const;

//Mutator functions
void setName(string);

void setIncome(double);

void setTax(double);

double calculateTax() const;

private:

//Member variables
string newName;
double newIncome;
double newTax;
};

#endif

Tax.cpp

     #include "Tax.h"

Tax::Tax()
{
newIncome = 0.0;
newTax = 0.0;
}

Tax::Tax(string name, double income, double tax)
{
newName = name;
newIncome = income;
newTax = tax;
}

Tax::~Tax()
{
}

string Tax::getName() const
{
    return newName;
}

double Tax::getIncome() const
{
return newIncome;
}

double Tax::getTax() const
{
return newTax;
}

void Tax::setName(string name)
{
newName = name;
}

void Tax::setIncome(double income)
{
    newIncome = income;
}
void Tax::setTax(double tax)
{
newTax = tax;
}

 double Tax::calculateTax() const
{
return (( newIncome - ( newIncome * (newTax / 100))) / 12); // ((68400 - ( 68400 * (38/100 = 0.38))) / 12)
}
Joe
  • 41,484
  • 20
  • 104
  • 125
cppnewb
  • 1
  • 3
  • 1
    What error do you get? – Joe Feb 04 '14 at 02:13
  • Hey Joe. I can compile the program fine. But after i exit it, i get the following error: "unhandled exception 0xC000005" (i think its some memory error). And it directs me to a "free.c" file where it says " if(retval == 0) { errno = errno = _get_errno_from_oserr(GetLastError()); } " the whole thing is included at the top of the question. PS. sorry if i sound abit out of context but im really tired – cppnewb Feb 04 '14 at 02:22
  • You don't do any dynamic allocation, and everything else looks ok to me after a few read-throughs... so I'm stumped. – Joe Feb 04 '14 at 02:29
  • Are you sure the code that generates the error is what you have above? And that you're not doing something odd like calling `delete` in your destructor or elsewhere? – Joe Feb 04 '14 at 02:32
  • I just tried to make a new project and copy paste the code above, compiled and the same error occurred, after i closed down the console window. So nope i haven't changed anything. – cppnewb Feb 04 '14 at 02:35
  • And now i just tried to compile in release mode, didn't get any error Oo is there a coincidence here or? is it my debugging settings that are wrong or ? – cppnewb Feb 04 '14 at 02:41

1 Answers1

0

I've (slightly) modified your code to work out, try this:

tax.h:

#include <iostream>
#include <string>

#ifndef TAX_H
#define TAX_H

class Tax
{
public:

    //Default constructor
    Tax();

    //Overload constructor
    Tax(std::string, double, double);

    //destructor
    ~Tax();

    //Accessor functions
    std::string getName() const;

    double getIncome() const;

    double getTax() const;

    //Mutator functions
    void setName(std::string);

    void setIncome(double);

    void setTax(double);

    double calculateTax() const;

private:

    //Member variables
    std::string newName;
    double newIncome;
    double newTax;
};

#endif

tax.cpp:

#include "tax.h"

Tax::Tax()
{
    newIncome = 0.0;
    newTax = 0.0;
}

Tax::Tax(std::string name, double income, double tax)
{
    newName = name;
    newIncome = income;
    newTax = tax;
}

Tax::~Tax() {}

std::string Tax::getName() const
{
    return newName;
}

double Tax::getIncome() const
{
    return newIncome;
}

double Tax::getTax() const
{
    return newTax;
}

void Tax::setName(std::string name)
{
    newName = name;
}

void Tax::setIncome(double income)
{
    newIncome = income;
}

void Tax::setTax(double tax)
{
    newTax = tax;
}

double Tax::calculateTax() const
{
    return (( newIncome - ( newIncome * (newTax / 100))) / 12);
}

main.cpp

#include "tax.h" // <iostream> and <string> are here
// never put 'using namespace std' in a header file

using namespace std;

int main()
{
    string name;
    double tax;
    double income;

    cout << "Enter your name: ";
    cin >> name;
    cout << "Enter your annually income: ";
    cin >> income;
    cout << "Enter your tax rate in pure numbers: ";
    cin >> tax;

    Tax Customer_1(name, income, tax);

    cout << endl << "Customer name: " << Customer_1.getName() << endl <<
        "Income annually: " << Customer_1.getIncome() << endl <<
        "Tax percent(in real numbers): " << Customer_1.getTax() << endl <<
        "Your income after tax, per month is: " << Customer_1.calculateTax() << endl;

    cout << endl;
    return 0;
}

Take note of what has been replaced and the headers you were using; judging by the _getch() at the end of the code, I'm assuming you were using that so that your console window would not go away while debugging; if that was the case, you could instead do something with cin and the strings you already have like so:

...
cin.clear(); // clear buffer for erroneous 'enter' presses
cin >> name; // console will pause until you press enter
return 0;

Though if your intention was to actually have the console pause (vs. the program just ending), you might want to consider other (more elegant) solutions but that is beyond the scope of this question.

A couple of notes: the reason your program worked when not in debug mode (i.e. you went to 'release') is your inclusion of the <crtdbg.h> header; this file is only valid in a debug configuration and you really shouldn't include it if your not using anything in it (which you're not in your code). I also took out the using namespace std; in your tax.h file as there are numerous reasons not to include using namespace std in a header.

I hope that can help

Community
  • 1
  • 1
txtechhelp
  • 6,625
  • 1
  • 30
  • 39
  • Hey txtechhelp. Thanks for your reply and your advices. I will take note of these in the future, and also tried to do so with the current project we're on. But unfortunately i still get the same error :/ I am on visual studio 2010, and i tried to change to 2012, where i again got an error. This time the little error arrow directs me to a text called (_Ptr) and when i hover over it, it says: Facet_Base::Vector deleting destructor(unsigned int). What exactly does that mean? should i change the type since it can't hold the number, or what is happening? thx for ur help – cppnewb Feb 04 '14 at 14:52
  • Can you compile from the command line? You can check by opening a command prompt and typing `cl`, if it says [`Microsoft C/C++ Compiler`](http://msdn.microsoft.com/en-us/library/ms235639.aspx) then you can try a command line compile of the simple tax.cpp and main.cpp files, ex: `cl.exe tax.cpp main.cpp /EHsc` .. this should produce a main.exe you can run. If the command line compile succeeds and there aren't any errors (as it should be) then Visual Studio is probably tacking on a lot more compiler options (via the project settings/etc) than you might think/need. – txtechhelp Feb 04 '14 at 23:07
  • hmm nope can't even compile from the command line. It just says "ERROR: Cannot determine the location of the VS Common Tools folder." at the top, and when i try to write cl it says it was not recognized as an intern or extern command, program or batchfile – cppnewb Feb 05 '14 at 00:02