Hi,Im having a hard time understanding the fundamentals of Object oriented programming.Ive made a really simple program that allows a user to enter a number, and then i have a function that doubles this number and returns it.

But im still having the same errors that i had when i tried to do my labwork,im wondering if you guys could take a look and see what mistakes im making.

Many thanks

MAIN.CPP

// program that prompts user to enter a numer
// function then called to double number
// and return it

#include <iostream>
#include "Number.cpp"
using namespace std;

main(){

    Number* number; // i have no idea what this does!I think its a variable declared.

    int input;

    cout << "Welcome to the double your number program." << endl;
    cout << "Begin by entering a number: ";
    cin >> input;

    // create a new object of class
    number = new Number();

    // call the function
    number->doubleNumber(input);
    cout << "Your number doubled is: " << endl;

}

HEADER FILE

#ifndef NUMBER_H
#define NUMBER_H

class Number(){

    public:
        Number(); // Default constructor
        int doubleNumber(int);

    private:
        int num; //will be used for the doubleNumber function
}


#endif

SOURCE FILE FOR FUNCTION

#include "Number.hpp" // MUST INCLUDE HEADER FILE

Number::Number() {
    num = 0;
}

Number::doubleNumber(int input) {
    num = input; // numnber passed in(input variable),is assigned to num variable.
    num = num*2; // algo
    return num;
}

Recommended Answers

All 8 Replies

code snippet 1: line #24

u are not printing the value.

/**do it as follows/
cout << "Your number doubled is: " <<number<< endl;

code snippet 2: line #4

sysntax error

/*correct  form is*/
class Number{/*() not required*/
}

Just a couple things I noticed right off, dkalita mentioned a few others

1.) You need to declare your main as int main() and then add a return 0; at the end of the function. The main must return an integer.

2.) Your includes are messed up. You need to include your header (which should have the extension .h not .hpp) in at least the implementation file ( *.cpp file). Normally you would include it in the main file as well. You shouldn't need to include it in the main since you included the implementation file instead.

Thanks for the help guys, i realise its not printing out i ust wanted to get to the stage were everything compiles.

So far iv gotten to fixing the class by removing the (), but im still getting errors with the constructors telling me "New types cannot be defined in the return type"(for default constructor)

Why does my main need to be type int with a return zero?We dont practice this in college but i've seen it online and wondered what it is.

Also,iv done the #includes the exact same way we're being thought,now im starting to lose confidence in these methods.Should i just rename my #include to .h?

Thanks for the help guys, i realise its not printing out i ust wanted to get to the stage were everything compiles.

So far iv gotten to fixing the class by removing the (), but im still getting errors with the constructors telling me "New types cannot be defined in the return type"(for default constructor)

Why does my main need to be type int with a return zero?We dont practice this in college but i've seen it online and wondered what it is.

Also,iv done the #includes the exact same way we're being thought,now im starting to lose confidence in these methods.Should i just rename my #include to .h?

You need a semi-colon after the closing brace of your class definition in your header file.

//header file "header_file.h"
class myClass{
  private:
    ...
  public:
    ...
}; //<-- required semi-colon

If your instructor is telling you to name your headers *.hpp continue to do that, you don't want to loose points. I would suggest double-checking your notes though, it's not the norm. Just be sure the filenames in your #include "*.hpp" statements match your actual filenames.

It is critical that your implementation file correctly include the header file, if it doesn't, the implementations can't compile because the compiler and/or linker won't have any clue where they belong to and what class they are for.

I got the semi colon worked out(im not going to forget that again in a hurry.)

Now however,im getting ONE error,a strange one saying

No reference to '_winmain@16'

I got the semi colon worked out(im not going to forget that again in a hurry.)

Now however,im getting ONE error,a strange one saying

No reference to '_winmain@16'

Did you set your program up as a Win32 CONSOLE application or as a windows application. It sounds like the linker is looking for the wrong program entry point...

"winmain" is the name of the function that Windows programs tend to use instead of just simply main. That is a project configuration issue, not a file issue. I'd suggest creating a new project, being sure to select "Console Application" (or whatever your particular IDE calls that type of program), then adding your files to the new project.

Did you set your program up as a Win32 CONSOLE application or as a windows application. It sounds like the linker is looking for the wrong program entry point...

"winmain" is the name of the function that Windows programs tend to use instead of just simply main. That is a project configuration issue, not a file issue. I'd suggest creating a new project, being sure to select "Console Application" (or whatever your particular IDE calls that type of program), then adding your files to the new project.

Yeah I started the program by using blank files in codeblocks.I used dev compiler to compile my code and its all good now. :)

I do still have a question though,passing a number back INTO my main,from the function,how would i go about displaying it in the cout

// call the function
    number->doubleNumber(input);
    cout << "Your number doubled is: " << endl;

im at a loss with this one

Try

// call the function
   cout << "Your number doubled is: " << number->doubleNumber(input) << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.