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

Unresolved external symbol "public: class Foo __thiscall Bar::bu(char const *)"

本文关键字:bu Bar thiscall const char class 外部 符号 public Foo      更新时间:2023-10-16

这是我得到的。我正在尝试使用自定义构造函数在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)
{
}

这可能就是您想要的:

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++的书,比如《C++入门》是个好主意。