Jump to content
  • Advertisement
Sign in to follow this  
Decrius

C++ definition error

This topic is 3786 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, I have three files, common.h, container.h and container.cpp. Common.h defines a structure called Info, which hold a pointer to the class Container. Container.h defines a class called Container, which hold an Info object. Container.cpp fills the Info object, by passing 'this' to fill the pointer in Info to it-self. The (parent) container class has a vector of (sub) container classes, in these sub container classes the Info object should have a pointer to the parent container. Thats why I fill the Info object with 'this' from within the parent. The error I get, is that the functions in Info are not defined.
// common.h

#ifndef COMMON_H
#define COMMON_H

#include <string>

class Container;

struct Info
{
    int id;
    std::string name;
    Container *parent;

    Info() : id(-1), parent(NULL) {}
    Info(int id, Container *parent) : id(id), parent(parent) {}
    Info(int id, std::string name, Container *parent) : id(id), name(name), parent(parent) {}
};

#endif // COMMON_H
// container.h

#ifndef CONTAINER_H
#define CONTAINER_H

#include <vector>

#include "gui/common.h"

class Container
{
public:
    void add(Container &iitem); // add container

    Info info;

private:
    std::vector<Container *> containers;
};

#endif // CONTAINER_H
// container.cpp

#include "gui/container.h"

void Container::add(Container &iitem)
{
    Container *item = new Container;
    *item = iitem;

    item->info((int) layers.size(), this);
}
Errors I get with the MingW compiler:
E:\C\GUI\gui\container.cpp: In member function `void Container::add(Container&)':
E:\C\GUI\gui\container.cpp:92: error: no match for call to `(Info) (int, Container* const)'
What could be possibly wrong? I'm out of idea's =/ Thanks, Decrius

Share this post


Link to post
Share on other sites
Advertisement
the compiler is right about the line number and error.

You try to access a constructor but at this time the object already exists
so you can´t call it:
item->info((int) layers.size(), this);


Something like that may work:
(If everything is correct a temporary Info object is created and you assign
the Info object from the class the values of the temp-Object)

item->info = Info((int) layers.size(), this);

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!