Reputation: 15
So I'm trying to write a function called "splitoList" to take 3 arguments, a string, another string that defines where to split the first string, and a class List to append the data to. Here's what I have so far.
split.h
#include <iostream>
#include <sstream>
using namespace std;
string splitoList(string s, string spechar, List <string> lst) {
size_t pos = 0;
string token;
int i = 0;
if (spechar == "") {
for (char i : s) {
string n(1,i);
lst.append(n);
}
return s;
} else {
while ((pos = s.find(spechar)) != std::string::npos) {
token = s.substr(0, pos);
lst.append(token);
s.erase(0, pos + spechar.length());
}
//cout << s;
lst.append(s);
lst.print();
return s;
}
}
lists.h
#include <iostream>
#include <cstdarg>
using namespace std;
template <class T>
class List {
public:
int size;
T* list;
List() {
size = 0;
list = new T[100];
}
void append(T data) {
list[size] = data;
size++;
}
int select(int pos) {
return list[pos];
}
void clear() {
for (int i = 0; i <= size; i++) {
list[i] = list[i - 1];
}
size = 0;
}
void sort(bool reverse) {
int temp;
if (reverse == true) {
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (list[i] > list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
else {
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (list[i] < list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
}
void remove() {
if (size == 0) {
cout << "[ ]\n";
return;
}
else {
for (int i = 0; i < size - 1; i++) {
list[i] = list[i + 1];
}
size--;
}
}
void reverse() {
int temp;
for (int i = size - 1; i > 0; i--) {
temp = list[i];
list[i] = list[size-i-1];
list[size-i-1] = temp;
}
}
void count(int item) {
int cnt = 0;
for (int i = 0; i < size; i++) {
if (list[i] == item) {
cnt++;
}
}
cout << cnt << endl;
}
void print() {
if (size == 0) {
cout << "[ ]\n";
return;
}
cout << "[";
for (int i = 0; i < size; i++) {
if (i == size - 1) {
cout << list[i];
}
else {
cout << list[i] << ", ";
}
}
cout << "]\n";
}
List operator+(List l2) {
List l3;
for (int i = 0; i < this->size; i++) {
l3.append(this->list[i]);
}
for (int i = 0; i < l2.size; i++) {
l3.append(l2.list[i]);
}
return l3;
}
};
and main.cpp
#include <iostream>
#include <sstream>
#include "lists.h"
#include "split.h"
using namespace std;
int main() {
List <string> l1;
l1.append("Hello!"); l1.append("World!");
l1.print();
l1.clear();
string n = "Hello World";
splitoList(n, " ", l1);
l1.print();
return 0;
}
I've deduced that it's not an issue with my "lists.h" file, but that the function outputs the list as intended but isn't appending to my list "l1" defined in main. So when lst.print(); is called in the function, it outputs "[Hello, World]" and when l1.print(); is called in main nothing is printed. I'm kinda stuck on this as I know it revolves around pointing to the inputted list class but I'm not sure what to do from here.
Upvotes: 0
Views: 21
Reputation: 6326
You need to change your function's prototype, with a reference argument, then the print statement works as you expected
string splitoList(string s, string spechar, List <string>& lst)
When passing lst
as value, the instance in function splitoList
is a copy of the one in your main function; as a reference, they are the same instance.
FAQs on reference:
https://isocpp.org/wiki/faq/references
Upvotes: 2