寫了一個決議GPS電文的類,但是測驗時候一直報錯
C:\Users\LeoYAO\AppData\Local\Temp\cc1ecCYe.o: In function `main':
F:/Coding/Cpp/Project/receiver/test_main.cpp:8: undefined reference to `StrSentence::StrSentence(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:\Users\LeoYAO\AppData\Local\Temp\cc1ecCYe.o:test_main.cpp:(.rdata$.refptr._ZTV11StrSentence[.refptr._ZTV11StrSentence]+0x0): undefined reference to `vtable for StrSentence'
collect2.exe: error: ld returned 1 exit status
原始碼
#ifndef MSGSENTENCEBASE_H
#define MSGSENTENCEBASE_H
#include <string>
class MsgSentenceBase // 抽象基類
{
public:
virtual ~MsgSentenceBase() { };
virtual std::string content() const = 0;
virtual int length() const = 0;
virtual bool SumChk() const = 0;
};
#endif
#ifndef STRSENTENCE_H
#define STRSENTENCE_H
#include "MsgSentenceBase.h"
#include <regex>
#include <bitset>
#include <iostream>
#include <vector>
#include <memory>
class StrSentence: public MsgSentenceBase
{
public:
StrSentence(const std::string&);
std::string content() const override { return sentence; }
int length() const override { return sentence.size(); }
bool SumChk() const override;
private:
std::string sentence;
std::shared_ptr<std::vector<std::string>> part_vec;
};
#endif
#include "StrSentence.h"
using std::vector;
using std::string;
using std::shared_ptr;
using std::make_shared;
using std::stoi;
using std::bitset;
using std::regex;
using std::sregex_token_iterator;
StrSentence::StrSentence(const string& s)
: sentence(s), part_vec(new vector<string>)
{
regex reg("[$,*]");
sregex_token_iterator beg(sentence.begin(), sentence.end(), reg, -1); // -1, 將正則式匹配的項作為分隔符
auto end = std::sregex_token_iterator(); // 默認建構式 end-of-sequence iterator
beg++; // 省略空項
for (; beg != end; ++beg)
{
part_vec->push_back(beg->str());
}
}
bool StrSentence::SumChk() const
{
auto check_sum = stoul(part_vec->back(), nullptr, 16);
auto str_to_chk = sentence.substr(1, sentence.size()-4);
auto beg_str = str_to_chk.cbegin();
auto end_str = str_to_chk.cend();
bitset<8> sum(0x00);
for (; beg_str!=end_str; beg_str++)
{
sum ^= bitset<8>(*beg_str);
}
return sum.to_ulong() == check_sum;
}
#include"StrSentence.h"
using namespace std;
int main()
{
string stc = "$xxxxxxxxxx";
StrSentence s(stc);
system("pause");
}
uj5u.com熱心網友回復:
我用的是dev c++加編譯選項:-std=gnu++11
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include <string>
#include <regex>
#include <bitset>
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class MsgSentenceBase // 抽象基類
{
public:
virtual ~MsgSentenceBase() { };
virtual std::string content() const = 0;
virtual int length() const = 0;
virtual bool SumChk() const = 0;
};
class StrSentence: public MsgSentenceBase
{
public:
StrSentence(const std::string&);
std::string content() const override { return sentence; }
int length() const override { return sentence.size(); }
bool SumChk() const override;
private:
std::string sentence;
std::shared_ptr<std::vector<std::string>> part_vec;
};
StrSentence::StrSentence(const string& s)
: sentence(s), part_vec(new vector<string>)
{
regex reg("[$,*]");
sregex_token_iterator beg(sentence.begin(), sentence.end(), reg, -1); // -1, 將正則式匹配的項作為分隔符
auto end = std::sregex_token_iterator(); // 默認建構式 end-of-sequence iterator
beg++; // 省略空項
for (; beg != end; ++beg)
{
part_vec->push_back(beg->str());
}
}
bool StrSentence::SumChk() const
{
auto check_sum = stoul(part_vec->back(), nullptr, 16);
auto str_to_chk = sentence.substr(1, sentence.size()-4);
auto beg_str = str_to_chk.cbegin();
auto end_str = str_to_chk.cend();
bitset<8> sum(0x00);
for (; beg_str!=end_str; beg_str++)
{
sum ^= bitset<8>(*beg_str);
}
return sum.to_ulong() == check_sum;
}
int main(int argc, char** argv) {
string stc = "$xxxxxxxxxx";
StrSentence s(stc);
system("pause");
return 0;
}
uj5u.com熱心網友回復:
鏈接錯誤undefined reference to `std::__cxx11::basic_string<char,https://www.cnblogs.com/lukybee/p/11846889.html
uj5u.com熱心網友回復:
vs2017能編譯,能運行uj5u.com熱心網友回復:
原來是Vscode沒有編譯全部的源檔案,得用CMake編譯一下,剛剛學C++不太懂,謝謝大家了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/278589.html
標籤:C++ 語言
上一篇:求大佬 ascii碼轉換
下一篇:列車問題求助
