0

I'm new to C++ and am running through a very simple file. I have one main file and a class (header + src file). however, whenever I run it I get an error.

Here's the source:

main.cpp

#include <iostream>
#include "Gui-Test/div.h"

using namespace std;

int main()
{
    div computer;
    cout << computer.divide();
    return 0;
}

div.h

#ifndef DIV_H
#define DIV_H


class div
{
    public:
        div();
        virtual ~div();
        int divide();

    protected:

    private:
};

#endif // DIV_H

div.cpp

#include "Gui-Test/div.h"

div::div()
{
    //ctor
}

div::~div()
{
    //dtor
}

int div::divide()
{
    return 6;
}

I'm using Gnu Make to compile it which gives the following error when trying to compile the main file into an object

g++ -c src/main.cpp -o objects/main.o -I include
src/main.cpp: In function ‘int main()’:
src/main.cpp:8:9: error: expected ‘;’ before ‘computer’
     div computer;
         ^~~~~~~~
src/main.cpp:8:17: error: statement cannot resolve address of overloaded function
     div computer;
                 ^
src/main.cpp:9:13: error: ‘computer’ was not declared in this scope
     cout << computer.divide();
             ^~~~~~~~
Makefile:17: recipe for target 'objects/main.o' failed
make: *** [objects/main.o] Error 1

Tried a few things and googled the hell out of it, but for some reason I'm not getting it to compile.

Can anyone assist?

Riaz Shageer
  • 141
  • 1
  • 15
  • 4
    `div` is a standard library function.Try renaming your class to `Div` (or something more descriptive). – ChrisD Oct 20 '18 at 17:11
  • 1
    @drescherjm No its not......it happens even without (I just tested it)..... – DarthRubik Oct 20 '18 at 17:13
  • 1
    I think `div` is a C library function which means that it doesn't have to be wrapped in a namespace – DarthRubik Oct 20 '18 at 17:14
  • Thanks @ChrisD that worked. – Riaz Shageer Oct 20 '18 at 17:16
  • 2
    np. You could also replace `using namespace std` with `using std::cout`. It's generally good practice to just bring only what you need into the global namespace. Alternatively you can define `class div` within your own namespace and then refer to it as `mynamespace::div computer`. – ChrisD Oct 20 '18 at 17:30

0 Answers0