AbsoluteZ3r0
AbsoluteZ3r0

Reputation: 119

C++ header issue involving functions and scope

My problem is in the following C++ code. On the line with the 'cout' I get the error:

"'number' was not declared in this scope".

.h

using namespace std;
class a{
     int number();
};

.cpp

using namespace std;
#include <iostream>
#include "header.h"

int main(){
    cout << "Your number is: " << number() << endl;
    return 0; 
}

number(){
    int x = 1;
    return x;
}

Note: I'm aware this isn't the cleanest code. I just wanted to get the function working and refresh my memory on how to use headers.

Upvotes: 1

Views: 73

Answers (3)

KRR
KRR

Reputation: 29

There are three issues with your code.

  1. The definition of the function number().

    As you declared, it is a member function of the class "a". In your .cpp, the class name should be used as a prefix to the function. I mean,

    a::number(){
        int x = 1;
        return x;
    }
    
  2. As the function is a member of the class "a", there are only two ways of accessing it,

    1. If the function is a static function in the class, you can access it with :: operator. Like a::number().
    2. If the function is not a static function, that is true in your case, you should instantiate the object out of the class "a" and they use "." operator with the reference. I mean,

      a obj;
      obj.number().
      
  3. Your function number() is declared in private scope. You may recall that by default the scope is a class is private unless you specify public or protected. So the private function number() cannot be used outside the declared class unless there is a friend to it.

    Below the code that I fixed,

    .h

    using namespace std;
    class a{
    public:
        int number();
    };
    

    .cpp

    using namespace std;
    #include <iostream>
    #include "header.h"
    
    a::number(){
        int x = 1;
        return x;
    }
    
    int main(){
        a obj;
        cout << "Your number is: " << obj.number() << endl;
        return 0; 
    }
    

Upvotes: 0

Arun
Arun

Reputation: 20403

For minimal fix, three basic changes are necessary.

Proper implementation of the number() method

int a::number() {
    int x = 1;
    return x;
}

Proper invocation of the number() method

a aObject;
cout << "Your number is: " << aObject.number() << endl;

There are many other enhancements possible though.


Addition, as pointed out by @CPlusPlus, usable scope of number() method, for example declaring it public

class a{
  public:
     int number();
};

Upvotes: 1

Rajeshwar
Rajeshwar

Reputation: 11681

Try this in your cpp file

using namespace std;
#include <iostream>
#include "header.h"

void a::number()
{
    int x = 1;
    return x;
}


int main()
{

cout << "Your number is: " << a().number() << endl;
return 0; 
}

As for your header file replace class with a struct. The reason you are getting this error is because the compiler cant find the variable number. It is actually a method of a class.The reason you are replacing the class with a struct is because by default everything in a struct is public. So your header file called header.h should look like this

using namespace std;
struct a
{
     int number();
};

Upvotes: 0

Related Questions