how come header variables are not recognized in its cpp file?

Feb 24, 2014 at 1:21am
I declared 3 private variables in header file.

I try to access one of the variable in its corresponding cpp file but I get an error message saying it's undefined. I did #include the header file. Why is this?
Feb 24, 2014 at 1:46am
closed account (3hM2Nwbp)
Are your accesses to the variables properly qualified? If you're not working in the same scope, without any using-declarations, then you will have to explicitly add the namespace to qualify access to the variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// example.hpp
namespace mynamespace
{
 class Example
 {
   int x; 
   static int y;
  public:
   Example(int x); 
 };
}

// example.cpp

int mynamespace::Example::y = 4;

mynamespace::Example::Example(int x) : x(x)
{

}

void func()
{
  int local_copy_of_y = mynamespace::Example::y;
}
Feb 24, 2014 at 2:04am
Thanks for your help. Unfortunately, it's greek to me what you just said. Here is my code

BELOW IS THE HEADER FILE Car.h

#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class Car
{
// Public Interface

private:


// Variable Declarations
int year, speed;
std::string make;


public:
// Class Constructor(s)
Car(int year, string make)
{

year = year;
speed = 0;
make = make;


}




// Class Destructor
~Car(void);

// Client Methods

int accelerate(int speed);
int brake(int speed);

int ObtainYr();
string ObtainMake();
int ObtainSpeed();
int Accelerate();
void getDisplay();




};

====================================
Below is the Car.CPP

#include "Car.h" // Class Definition file
#include <stdio.h>
#include "stdafx.h"
#include <string.h>


// Class Default Constructor

Car::Car(int Yr, string Mk, int Spd)
{
Year = Yr;
Make = Mk;
Speed = Spd;
}

===========
problem is the visual studio won't recognize Year, Make, Speed. Why?
Feb 24, 2014 at 2:16am
closed account (3hM2Nwbp)
C++ is case sensitive. In your header file, your private members are 'year', 'make', and 'speed'...however in your implementation file, you refer to them as 'Year', 'Make', and 'Speed'.
Feb 24, 2014 at 5:59am
Thanks Luc Lieber. I overlooked it.
Topic archived. No new replies allowed.