Q:

"undefined identifier" despite defining it in the class.

im a beginner in c++ and i am so confused why i am getting an error in my code, could you guys please tell me whats going wrong? im using visual studios 2017.

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

class Cat {
private:
bool happy;
public:
void speak() {
    cout << "meow" << endl;
}
Cat() { 
    bool newHappy = happy;
    happy = true;
}
};

int main()
{
    cout << "Starting program..." << endl;

    Cat bob;
    bob.speak();

    if (happy) {
        cout << "cat is happy" << endl;
    }
    else {
        cout << "unhappy cat" << endl;
    }

    cout << "Ending program..." << endl;
    return 0;
}

A:

You're trying to reference a variable called happy inside your main function, which doesn't exist in that scope. If you want to see if bob is happy, you could simply write if (bob.happy){ ... and change Cat::happy from private to public, or you could create a getter function like:
class Cat {
    private:
    bool happy;
    public:
    bool isHappy() const {
        return happy;
    }
    ...
};

and call the function as follows: if (bob.isHappy()){ ...

