Sign in to follow this  
Decrius

C++ definition error

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this