not declared in this scope yet I am referring to the header file

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 5491 Views - Last Post: 18 December 2012 - 08:30 AM Rate Topic: -----

#1 BreaksOnDust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 09-December 12

not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 05:11 PM

Why does it keep giving me this error?

Error message
C:\Users\User\Desktop\C++\Pass by Reference Pointers\passByClass.h|13|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Users\User\Desktop\C++\Pass by Reference Pointers\passByClass.h|14|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Users\User\Desktop\C++\Pass by Reference Pointers\passByClass.cpp||In function 'void printResult()':|
C:\Users\User\Desktop\C++\Pass by Reference Pointers\passByClass.cpp|14|error: 'apple' was not declared in this scope|
C:\Users\User\Desktop\C++\Pass by Reference Pointers\passByClass.cpp|14|error: 'passByValue' was not declared in this scope|
C:\Users\User\Desktop\C++\Pass by Reference Pointers\passByClass.cpp|15|error: 'orange' was not declared in this scope|
C:\Users\User\Desktop\C++\Pass by Reference Pointers\passByClass.cpp|15|error: 'passByReference' was not declared in this scope|
||=== Build finished: 4 errors, 2 warnings (0 minutes, 0 seconds) ===|



main.cpp
#include <iostream>
#include "passByClass.h"
using namespace std;



int main()
{
    passByClass result;
    result.printResult();
    return 0;
}



passByClass.h
#ifndef PASSBYCLASS_H
#define PASSBYCLASS_H


class passByClass
{
    public:
        void passByValue(int x);
        void passByReference(int *x);
        void printResult();

    private:
        int apple = 15;
        int orange = 15;
};

#endif // PASSBYCLASS_H


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

void passByClass::passByValue(int x){
    x = 99;
}

void passByClass::passByReference(int *x){
    *x=66;
}

void printResult(){
    passByValue(apple);
    passByReference(&orange);

    cout << "apple is now " << apple << endl;
    cout << "orange is now " << orange << endl;
}



Don't know what I'd do without you guys! :wub:

Is This A Good Question/Topic? 0
  • +

Replies To: not declared in this scope yet I am referring to the header file

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 05:21 PM

On line 13 you're not defining the member function passByClass::printResult, you're defining a free function printResult that has no connection to the passByClass class. That's why you can't access the member variables apple and orange.

This post has been edited by sepp2k: 17 December 2012 - 05:21 PM

Was This Post Helpful? 1
  • +
  • -

#3 BreaksOnDust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 09-December 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 05:33 PM

View Postsepp2k, on 17 December 2012 - 05:21 PM, said:

On line 13 you're not defining the member function passByClass::printResult, you're defining a free function printResult that has no connection to the passByClass class. That's why you can't access the member variables apple and orange.


Now I see! Thank you!

Also why is it that when passing a variable to the passByReference-function I get to use the asterisk twice in that context,

whereas in the following example i'm not allowed to put an asterisk infront of fishpointer.,

#include <iostream>
using namespace std;

int main()
{
    int fish = 5;
    cout << &fish << endl;

    int *fishPointer;
     fishPointer = &fish;

    cout << fishPointer << endl;
    return 0;
}




Why is that I get to put an asterisk infront of variable x two times when working with functions but only one time when not working with functions?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:36 PM

When declaring a variable/parameter, putting an asterisk in front of the variable name, makes that variable a pointer. When using a variable, putting an asterisk in front of the variable means that you're dereferencing the pointer (that is you're accessing the memory that the pointer points to). You may not dereference a pointer that does not point to a valid memory location.

So if you added an asterisk in front of fishPointer on line 10 of your code, that'd be illegal because fishPointer does not currently point to a valid memory location (you never assigned anything to it before line 10). If you added a 11th line after you've assigned fishPointer on line 10, it would be perfectly legal to add an asterisk. So for example the following would be perfectly okay:

int fish = 23;
int *fishPointer;
fishPointer = &fish;
*fishPointer = 42; // Same as writing: fish = 42;



Using functions makes no difference in that regard. In your code with functions you're assigning x the address of orange when you call the function and then you dereference x inside the function. If you called your function with an uninitialized pointer as its argument, that would be just as illegal as dereferencing your fishPointer before assigning an address to it.

This post has been edited by sepp2k: 17 December 2012 - 07:46 PM

Was This Post Helpful? 1
  • +
  • -

#5 BreaksOnDust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 09-December 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:45 PM

But I did add an asterisk infront of fishpointer on line 9? :o

And the code runs fine?
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:46 PM

I meant line 10, sorry.
Was This Post Helpful? 0
  • +
  • -

#7 CY5  Icon User is online

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 348
  • Joined: 28-September 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:46 PM

Quote

#ifndef PASSBYCLASS_H
#define PASSBYCLASS_H


class passByClass
{
public:
void passByValue(int x);
void passByReference(int *x);
void printResult();

private:
int apple = 15;//
int orange = 15;
};

#endif // PASSBYCLASS_H

do not give value to variable in the header file like above you mentioned:
int apple=15; int orange=15;in passByClass.h file and also Seep2k said was correct.I have got it working you try it.
Was This Post Helpful? 0
  • +
  • -

#8 BreaksOnDust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 09-December 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:48 PM

Yeah, I tried it. It works fine. Why is it bad practice to add value to variables declared in the header file?
Was This Post Helpful? 0
  • +
  • -

#9 CY5  Icon User is online

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 348
  • Joined: 28-September 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:54 PM

To learn more about this error visit the link
http://msdn.microsof...w(v=vs.80).aspx
Was This Post Helpful? 0
  • +
  • -

#10 BreaksOnDust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 09-December 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:55 PM

sepp2k, took a while and I had to re-read your previous post a couple of times but I finally understand. A pointer cannot be dereferenced before it has been assigned a value. Thanks mate! :D
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 07:55 PM

View PostBreaksOnDust, on 18 December 2012 - 03:48 AM, said:

Yeah, I tried it. It works fine. Why is it bad practice to add value to variables declared in the header file?


Two things: Giving values to non-static data members inside a class definition is simply not allowed by the standard prior to C++11. A standard-compliant pre-2011 compiler would be in its rights to reject that code. So if you're using C++03, it's not just bad practice - it's simply not allowed. If you're using C++11, it's perfectly okay.

Giving values to global variables or non-const static variables inside headers (outside of the class definition) is bad practice because it will lead to linker errors if you include the header file in more than one c++ file.
Was This Post Helpful? 1
  • +
  • -

#12 BreaksOnDust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 09-December 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 08:05 PM

So If I shouldn't assign them values inside the classes, where should I do it? Just... Globally in the main.cpp? And then pass them into the functions?

If so, how am I supposed to be able to assign values to private variables who's been declared in a class?
Was This Post Helpful? 0
  • +
  • -

#13 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 09:58 PM

View PostBreaksOnDust, on 18 December 2012 - 08:35 AM, said:

how am I supposed to be able to assign values to private variables who's been declared in a class?


You can do that by creating objects of the class and using the objects, you can initialize the private members of the class using the member functions.

Eg.

class BoxDimensions
{
   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );
 
   private:
      double width;
};
 
// defining the member functions 
double BoxDimensions::getWidth(void)
{
    return width ;
}
 
void Boxdimensions::setWidth( double wid )
{
    width = wid;
}
 
int main( )
{
   BoxDimensions newbox; // creating the object of the class
 
   newbox.length = 20.0; 
   newbox.width = 20.0; // Error will be thrown because 'width' is private
   newbox.setWidth(20.0);  // Use member function instead
   cout << "Width of box : " << newbox.getWidth() <<endl;
 
   return 0;
}


the output will be Width of the box : 20

Edit: Silly typo :P

regards,
Raghav

This post has been edited by raghav.naganathan: 17 December 2012 - 11:00 PM

Was This Post Helpful? 0
  • +
  • -

#14 CY5  Icon User is online

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 348
  • Joined: 28-September 12

Re: not declared in this scope yet I am referring to the header file

Posted 17 December 2012 - 10:57 PM

View PostBreaksOnDust, on 17 December 2012 - 08:05 PM, said:

So If I shouldn't assign them values inside the classes, where should I do it? Just... Globally in the main.cpp? And then pass them into the functions?

If so, how am I supposed to be able to assign values to private variables who's been declared in a class?

You can't declare variable with value in header file,you can only give value to static const datatype variable.So please declare the varaible in .cpp and not .h
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: not declared in this scope yet I am referring to the header file

Posted 18 December 2012 - 05:45 AM

View PostBreaksOnDust, on 18 December 2012 - 04:05 AM, said:

So If I shouldn't assign them values inside the classes, where should I do it?


Non-static member variables should be initialized using the class's constructor. Static variables should be defined in the c++ file that also contains your class's member function definitions. Likewise global variables should be defined in the c++ file that also contains the definitions of the functions defined in the header. It does not make a difference whether variables are private or not.

Example:

MyClass.h:
extern int global_var;

class MyClass {
private:
  static int x;
  int y;
  int z;

public:
  MyClass(int value_for_y);
};



MyClass.cpp:
#include "MyClass.h"

int global_var = 13;

int MyClass::x = 42;

MyClass::MyClass(int value_for_y)
  : y(value_for_y), z(23)
  {}



View Postraghav.naganathan, on 18 December 2012 - 05:58 AM, said:

You can do that by creating objects of the class and using the objects, you can initialize the private members of the class using the member functions.


That's assuming that every private member variable has a public setter method. That's not generally the case.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2