提问人:Jcrack 提问时间:2/3/2012 最后编辑:R. Martinho FernandesJcrack 更新时间:2/3/2012 访问量:695
未解析的外部符号“public: class Foo __thiscall Bar::bu(char const *)”
Unresolved external symbol "public: class Foo __thiscall Bar::bu(char const *)"
问:
这是我所拥有的。我正在尝试使用自定义构造函数在 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)
{
}
答:
0赞
Jesse Good
2/3/2012
#1
这可能是你想要的:
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)
{
}
至于这个声明:这是一个成员函数的声明,名为 接受 a 并返回 .未解决的符号错误是因为你从未定义过函数,但你想要的不是函数的实例。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++入门》会是个好主意。
评论