Reputation: 39
I have written function for splitting sentence to words, but something went wrong after I wanted to work with it. mainwindow.cpp :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <vector>
#include <iostream>
using namespace std;
vector <QString> myvector;
vector<string>::iterator it;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_addButton_clicked()
{
QString str = ui->textEdit->toPlainText();
//qDebug() << str;
vector<string> v = split(str.toStdString());
for(int i = 0; i < myvector.size(); i++){
ui->listWidget->addItem(myvector[i]);
}
}
vector<string> split(const string s)
{
vector<string> myvector;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size()) {
while (i != s.size() && isspace(s[i]))
++i;
string_size j = i;
while (j != s.size() && !isspace(s[j]))
j++;
if (i != j) {
myvector.push_back(s.substr(i, j - i));
i = j;
}
}
return myvector;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <vector>
#include <iostream>
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
std::vector<string> split(const string s);
private slots:
void on_addButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
here is error :
undefined reference to
MainWindow::split(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)
Upvotes: 0
Views: 826
Reputation: 28987
You declare a member function in MainWindow and then use it, but you define:
vector<string> split(const string s)
That is a standalone function. Probably easiest to change to:
vector<string> MainWindow::split(const string s)
A better solution though, would be to declare split
outside of the MainWindow
class (it has nothing to do with MainWindow, it is just a useful litte utility function), and leave the definition alone.
I would also change the argument to be const string& s
, to save having to copy the string (and I concur with Bathsheba - use a std::unique_ptr to take care of deleting).
Upvotes: 2