Jump to content
  • Advertisement
Sign in to follow this  
Lord_Danny

[C++] Help please - Class problems

This topic is 2607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

I'm pretty new to C++ and I am experiencing a problem and hoping someone can help me to solve this. I have created a class called Food which has a header file and a cpp file (food.hpp and food.cpp). I also have another cpp file called MainFrame.cpp, but this is not a class. The problem I'm stuck with is trying to create an food object in the MainFrame.cpp, but I keep getting errors. Can someone tell me how exactly do you create a object of a class (food) within an different cpp file please? Here's my codes:


//Food.hpp
#ifndef FOOD_H
#define FOOD_H

class Food
{
public:
Food(std::string setName, double setPrice);
~Food(void);

std::string name;
double price;
};

#endif

---------------------------------------------------------------------------------------------------

//Food.cpp
#include<string>
#include "Food.h"

Food::Food(std::string setName, double setPrice)
{
name = setName;
price = setPrice;
}

Food::~Food(void)
{
};

---------------------------------------------------------------------------------------------------

//MainFrame.cpp
#include<iostream>

using namespace std;

int main()
{
Food::food("beef", 3.80);
}

---------------------------------------------------------------------------------------------------

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Why do you have a : after the } in the Destructor? Try to remove it..

and you also have to create an instance of Food



int main ()
{
Food *food = new Food ("beef", 3.80);

// work with food

delete food; // when u don't need it anymore


return 1;
}

Share this post


Link to post
Share on other sites
Hi flyingdutchman,

Thanks for the reply. I have removed the ; after the destructor and tried the code you've suggested, but I am still getting the error. Heres the error messager: 'error C2653: 'Food' : is not a class or namespace name'

Do i need to include the food.cpp in my mainframe.cpp by any chance?

P.S. this food is expensive because it's organic ^^ lol

Share this post


Link to post
Share on other sites
You need to #include Food.h into MainFrame.cpp. You do not need dynamic allocation.

#include <iostream>
#include "Food.h"

using namespace std;

int main()
{
Food food("beef", 3.80);
std::cout << "We have some " << food.name << " for " << food.price << '\n';
}

Your food header should #include <string>, as it makes use of std::string. Your Food.cpp should include Food.h as the first line, that way you'll catch errors like omitting the <string> include.

You never include cpp files*.

* [size="1"]Unless you know what you're doing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!