"undefined identifier" despite defining it in the class. - c++

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;
}

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()){ ...

Related

Why copy constructor changed previous object pointer member

#include <iostream>
#include <string.h>
#include "Date.h"
#include "Employee.h"
using std::cout;
using std::endl;
using std::to_string;
class TestOps {
public:
int sex = 1;
string toString() {
return " sex:" + to_string(sex) ;
}
};
class Test {
public:
TestOps* testOps;
Test(const Test& t) :Test{} {
this->testOps = new TestOps{ *(t.testOps) };
};
Test() {
TestOps ops;
//this->testOps = new TestOps{}; // it will be ok with this way
this->testOps = &ops;
}
};
int main() {
// code not understand
Test t1;
cout <<"first testOps:" << t1.testOps->toString() << endl; // sex: 1
Test t2{ t1 };
cout << "first testOps:" << t1.testOps->toString() << endl; // sex: -858893460 ???? why?
cout << "second testOps:" << t2.testOps->toString() << endl; // sex: -858893460 ???? why?
return 0;
}
As you can see, why the first log is as expected while the later logs are not?
Also, t1.testOps address is different from t2.testOps which is as expected.
I have done some research but didn't find the answer. Maybe because I'm pretty new to cpp.

C++ How to implement this code into the heap properly

I am working on a class assignment to create three classes nested inside each other. I need to make constructors and deconstructors for each that have a message that goes along with them. Finally, I need to create an instance of each class using new and call the display() function to show their message, followed by delete.
I have completed the assignment but in the wrong way, and I am confused about how I can properly put the code into the heap instead of the stack (as I was advised by my course tutor).
This is what I started with: (this code seems to work well, but does not fulfill the assigned project)
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Hen {
public:
Hen();
~Hen();
string display(void) {
return ("Im a Hen");
}
class Nest;
friend Nest;
class Nest {
public:
Nest();
~Nest();
string display(void) {
return ("Im a Nest");
}
class Egg;
friend Egg;
class Egg {
public:
Egg();
~Egg();
string display(void) {
return ("Im an egg");
}
};
};
};
Hen::Hen() {
cout << "I construct Hens" << endl;
}
Hen::~Hen() {
cout << "I deconstruct Hens" << endl;
}
Hen::Nest::Nest() {
cout << "I construct Nests" << endl;
}
Hen::Nest::~Nest() {
cout << "I deconstruct Nests" << endl;
}
Hen::Nest::Egg::Egg() {
cout << "I construct Eggs" << endl;
}
Hen::Nest::Egg::~Egg() {
cout << "I deconstruct Eggs" << endl;
}
int main() {
Hen hone;
Hen::Nest none;
Hen::Nest::Egg eone;
string h, n, e;
h = hone.display();
n = none.display();
e = eone.display();
cout << h << "\n" << n << "\n" << e << endl;
}
Where I am stuck is when I try to implement my code inside the heap, it seems to break by the second class:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Hen {
public:
void display() {
cout << "Im a Hen" << endl;
}
class Nest;
friend Nest;
class Nest {
public:
void display() {
cout << "Im a Nest" << endl;
}
class Egg;
friend Egg;
class Egg {
public:
void display() {
cout << "Im an egg" << endl;
}
};
};
};
int main() {
Hen *hone = new Hen();
Hen::Nest *none = new Nest();
hone -> display();
none -> display();
}
Question 1:
If I remove all the information related to nest, the program runs Hen just fine and returns the "I'm a hen" statement. But, when I add in nest, the warning I recieve is
"error: expeected type-specifier before 'Nest'
Hen::Nest *none = new Nest();"
I do not understand what I am doing wrong as I did the exact same process for Hen and it worked. I do know that the error must be in the way Nest gets called through hen?
I apologize if this question is obvious, but I am just starting c++ and do not understand why I am getting these messages...
Thanks for your help!

How do I get my constructor and functions to work so my main() is able to display both the string and int data?

I am learning about functions and classes, and wrote my own code. I used the constructor to just initialize the variables. I have a function that is supposed to get the info I initialized with the constructor and allow me to display it. However, it doesn't want to work. I am not really sure what I am doing wrong. My error code says that I have unresolved externals because of my "void" function. I thought my function was not returning anything but rather just displaying the input it got from the initialization of the constructor.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Berries {
string Nameofberries;
int Price;
public:
Berries (string N,int B)
{
Nameofberries = N;
Price = B;
}
void GetBerryInfo(const Berries& B)
{
cout << B.Nameofberries << endl;
cout << B.Price << endl;
}
};
void GetBerryInfo (const Berries& B);
int main ()
{
Berries Berryinfo1( "Raspberries", 7);
cout << GetBerryInfo;
system("pause");
return 0;
}
There are several mistakes.
void GetBerryInfo(const Berries& B)
{
cout << B.Nameofberries << endl;
cout << B.Price << endl;
}
should be
void GetBerryInfo()
{
cout << Nameofberries << endl;
cout << Price << endl;
}
==================================================================
void GetBerryInfo (const Berries& B);
should be removed.
==================================================================
cout << GetBerryInfo;
should be
Berryinfo1.GetBerryInfo();
==================================================================
All computer langauges are fussy, you have to get the details right, as well as understand the concepts.
This will do what you wanted:
# include <iostream>
# include <iomanip>
# include <string>
using namespace std;
class Berries {
string Nameofberries;
int Price;
public:
Berries (string N,int B)
{
Nameofberries = N;
Price = B;
}
void GetBerryInfo()
{
cout << Nameofberries << endl;
cout << Price << endl;
}
};
int main ()
{
Berries Berryinfo1( "Raspberries", 7);
Berryinfo1.GetBerryInfo();
system("pause");
return 0;
}
A couple of points on your mistakes:
GetBerryInfo() was declared inside the class. You don't need to re-declare it in the global scope. That 2nd declaration should be removed.
To be invoked, functions (like GetBerryInfo) must have () at the end of them like so: GetBerryInfo().
There is no point for GetBerryInfo() to take Berries as a paremeter. It is a member function that is part of the class Berries. It has access to all data members of a Berries instance already.
You don't need to use cout here: cout << GetBerryInfo; because the function body already sends the data members to cout. This function returns void so it doesn't make sense to send this to cout anyway.

Cannot call function of a class from main

#include <iostream>
#include <string>
using namespace std;
// my code starts
class Cat {
public:
int age;
string name, race, voice;
Cat(int age2,string name2,string race2,string voice2);
void PrintInformation();
};
Cat::Cat(int age2,string name2,string race2,string voice2) {
age = age2;
name = name2;
race = race2;
voice = voice2;
}
Cat::Meow(){
cout << "Cat says: " << fluffy.Meow() << endl;
}
void Cat::PrintInformation() {
cout << "Name: " << name;
cout << "\nAge: " << age;
cout << "\nRace: " << race << endl;
}
// my code ends
int main()
{
Cat fluffy(2, "Fluffy", "Bombay", "Meoow!!!");
fluffy.PrintInformation();
cout << "Cat says: " << fluffy.Meow();
}
I can't seem to figure out how to make this code work. My main problems seems to be that i don't know how to call fluffy.Meow(); from int main().
Thanks, for any help!
You forgot to declare Cat::Meow in the class declaration.
//some code
void PrintInformation();
void Meow();
Additionally, you have to specify what the return type of the function Meow is, in your case it would be void, because it returns nothing.
You also have some recursion going on, Meow calling Meow (forgetting about the fact that fluffy isn't a variable in this scope). Your Cat class knows nothing about the instance fluffy, so you can't access it.
I guess you meant voice instead.

expected unqualified id before while c++

I am making a simple C++ game based loosely on Starcraft. This is a way to practice pointers.
The program works fine so now im adding the little technical stuff in this case, the Cloaking ability ghost
In the ghost class, I set up a while loop for while bool cloak == true , you set the hits to blank as the ghost cant be hit while cloaked ( no detectors in this game) When i set it up, it gives me the error " expected unqualified id before while" .If i take out the loop, it doesnt give me an error.
any help is much appreciated
here is my ghost.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "ghost.h"
ghost::ghost(string iname, string iteam, string itype, int Snipe, bool cloak)
: infantry(iname, iteam, itype)
{
set_SniperR(Snipe);
set_Cloak(cloak);
set_health(80);
}
void ghost::set_SniperR(int Snipe)
{
SniperR = Snipe;
}
int ghost::get_SniperR() const
{
return SniperR;
}
void ghost::shoot_SniperR(infantry* attacked_infantry)
{
if(SniperR!=0 && this->get_health()!=0 && attacked_infantry->get_health()!=0)
{
attacked_infantry->SniperR_hit();
}
}
void ghost::attack(infantry* attacked_infantry)
{
shoot_SniperR(attacked_infantry);
if (attacked_infantry->get_health() == 0)
attacked_infantry->die();
}
void ghost::heal(infantry* attacked_infantry) { }
void ghost::die()
{
set_SniperR(0);
}
void ghost::set_Cloak(bool cloak)
{
Cloak = cloak;
}
bool ghost::get_Cloak() const
{
return Cloak;
}
while ( cloak) // <-- error
{
void ghost::AssaultR_hit()
{
// when cloak is on , AssaultR doesnt affect Ghost
}
void ghost::FlameT_hit() { }
void ghost::SniperR_hit() { }
void ghost::RocketL_hit() { }
void ghost::StickyG_hit() { }
}
void ghost::print() const
{
cout << endl;
infantry::print();
cout << "Sniper Rifle Rounds: " << get_SniperR() << endl;
}
void ghost::speak() const
{
infantry::speak();
cout << "Did somebody call for an exterminator? " << endl;
}
void ghost::display() const
{
infantry::display();
cout << right << setw(5) << " "
<< right << setw(5) << " "
<< right << setw(10) << get_SniperR()
<< endl;
}
The proper way to do this is to remove the while loop and check if cloak is true in your methods. Here is a implementation that will not give you the errors(assuming cloak is a member variable, which it should be in this case):
void ghost::AssaultR_hit()
{
if(!cloak)
{
//assualtR_hit implementation goes here
}
}
void ghost::FlameT_hit()
{
if(!cloak)
{
//FlameT_hit implementation goes here
}
}
void ghost::SniperR_hit()
{
if(!cloak)
{
//SniperR_hit implementation goes here
}
}
void ghost::RocketL_hit()
{
if(!cloak)
{
//RocketL_hit implementation goes here
}
}
void ghost::StickyG_hit()
{
if(!cloak)
{
//StickyG_hit implementation goes here
}
}
Note: Also note the comment that you can not have a while loop outside of a function in C++ like the commentator said.