gpt4 book ai didi

c++ - C++中的类方法错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:49:47 25 4
gpt4 key购买 nike

声明类时出现错误:

#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;

int main(void)
{
test links;
string content="this is an string";
links.getcont(content);
}

测试.h

#ifndef TEST_H_
#define TEST_H_
#include<string>
using namespace std;
class test {
public:
string getcont(string content);

};

#endif /* TEST_H_ */

测试.cpp

#include "test.h"
#include <iostream>
using namespace std;
string getcont(string content)
{
cout << content;
return content;
}

当我运行它时,我得到了这个错误:

undefined reference to test::getcont(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)

最佳答案

那么,在您的 test.cpp 文件中为此替换 getcont 函数

string test::getcont(string content){ //code here; }

问题是你不是说 getcont 是测试类的成员函数。

此外,考虑使它成为一个 const 函数并传递一个 const 字符串引用

string
test::getcont( const string& content) const
{
return content;
}

关于c++ - C++中的类方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3638993/

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