gpt4 book ai didi

c++ - 未解析的外部符号 "public: int myclass::get_a (void)"如何解析此代码?菜鸟Q

转载 作者:行者123 更新时间:2023-11-28 02:31:03 26 4
gpt4 key购买 nike

这里是第一次学习 C++ 的新手程序员。以下是从 Teach Yourself C++ 第三版中摘录的代码。我很想帮助我,我正在学习类,但我无法让这段代码在 visual studio 或 Code::Blocks 上编译。 :(

//#include "stdafx.h"
#include <iostream>
//I understand this. Headers, etc.
using namespace std;
//and this, name traffic management system
class myclass {
//private to myclass
int a;
public:
void set_a(int num);
int get_a();
};
/*I understand int a is private/inaccessible from the rest of the code
and void set_a(int num) is the dummy function.*/
void myclass::set_a(int num)
//not sure what this is
{
a = num;
}
/*self explanatory*/
int _tmain(int argc, _TCHAR* argv[])
{
myclass ob1, ob2;

ob1.set_a(10);
ob2.set_a(99);

cout << ob1.get_a() << "\n";
cout << ob2.get_a() << "\n";

return -5;
}
/*This is just supposed to output the number 10 and 99 right?? So why isn't it?*/

在 Visual Studio 上,完整的错误描述是:错误 1 ​​error LNK2019: unresolved external symbol "public: int __thiscall myclass::get_a(void)"(?get_a@myclass@@QAEHXZ) referenced in function _wmain c:\Users\bernardo pliego\documents\visual studio 2013\Projects\Chapter 1.5\Chapter 1.5\Chapter 1.5.obj Chapter 1.5

在 Code::Blocks 上我收到以下错误:在函数“主要”中:对“my_class::get_a()”的 undefined reference

我急需帮助,有人能给我解释一下吗?

最佳答案

因为你没有定义get_a,你只是声明它。添加这样的定义:

int myclass::get_a() { return a; }

或者只是内联定义它:

class myclass {
//private to myclass
int a;
public:
void set_a(int num);
int get_a() { return a };
};

关于c++ - 未解析的外部符号 "public: int myclass::get_a (void)"如何解析此代码?菜鸟Q,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28991243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com