未解析的外部符号“public:class Foo __thiscall Bar :: bu(char const *)”

时间:2012-02-03 06:05:26

标签: c++ class unresolved-external

这就是我所拥有的。我正在尝试使用自定义构造函数在Bar中创建Foo实例。当我尝试从Bar的构造函数中调用构造函数时,我得到了外部未解析的。

class Foo
{
public:
    // The custom constructor I want to use.
    Foo(const char*);
};

class Bar
{
public:
    Bar();
    //The instance of Foo I want to use being declared.
    Foo bu(const char*);
};

int main(int argc, char* args[])
{
    return 0;
}

Bar::Bar()
{
    //Trying to call Foo bu's constructor.
    bu("dasf");
}

Foo::Foo(const char* iLoc)
{
}

1 个答案:

答案 0 :(得分:0)

这可能是你想要的:

class Foo
{
public:
    // The custom constructor I want to use.
    Foo(const char*);
};

class Bar
{
public:
    Bar();
   //The instance of Foo I want to use being declared.
   Foo bu; // Member of type Foo
};

int main(int argc, char* args[])
{
    return 0;
}

Bar::Bar() : bu("dasf") // Create instance of type Foo
{
}

Foo::Foo(const char* iLoc)
{
}

至于此声明:Foo bu(const char*);这是一个名为bu的成员函数的声明,它接受const char*并返回Foo。未解决的符号错误是因为您从未定义函数bu,但您希望Foo的实例不是函数。

看到评论后的其他方法:

class Foo
{
public:
    // The custom constructor I want to use.
    Foo(const char*);
};

class Bar
{
public:
    Bar();
    ~Bar() { delete bu;} // Delete bu
   //The instance of Foo I want to use being declared.
   Foo *bu; // Member of type Foo
};

int main(int argc, char* args[])
{
    return 0;
}

Bar::Bar() : bu(new Foo("dasf")) // Create instance of type Foo
{
}

Foo::Foo(const char* iLoc)
{
}

你的代码还有很多其他问题,也许就像C++ Primer那样获得一本关于C ++的书是个好主意。