subclassing the string class

hi,

As I am writing a program to handle some csv files, I wrote some functions that handle strings which I would like to add to the string class. So I am at the stage that I try to make a 'empty' subclass (without adding new functions to it for the moment), but I get an error which I can't seem to get rid off:
expected unqualified-id before 'using' (line 6 of the main file)

Here's my code

main file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <fstream>
#include <iostream>
#include <list>
#include <editedstring.h>

using namespace std;

list<EditedString> splitlist(EditedString,char);
void arraysplit(EditedString*, EditedString, char, int);

int main()
{
        EditedString path = ("...");
        char separator = '\n';
        EditedString str;

        ifstream ifile(path.c_str());
    if (ifile.is_open())
                {
                 // do stuff
                }
    else
                cout << "problem";

    int dummy;
    cin >> dummy;
    return 0;
}

//first function which I want to put in the subclass: splitting strings into lists
list<EditedString> listsplit(EditedString str_in, char sep_in)
{
        list<EditedString> splitlist;
        int pos = 0, length;
        while(str_in.find(sep_in, pos)!=EditedString::npos)
                {
                        length = str_in.find(sep_in, pos) - pos;
                        splitlist.push_back(str_in.substr(pos, length));
                        pos += length + 1;
                }
        splitlist.push_back(str_in.substr(pos,str_in.size()));	//what rests after the last separator

        return splitlist;
}

//second function which I want to put in the subclass: splitting strings into arrrays
void arraysplit(EditedString* str_array_in, EditedString str_in, char sep_in, int array_length)
{
        int pos = 0, ind3x = 0, substr_length;
        while((str_in.find(sep_in, pos)!=EditedString::npos) && ind3x < array_length)
                {
                        substr_length = (str_in.find(sep_in, pos) - pos);
                        str_array_in[ind3x] = str_in.substr(pos, substr_length);
                        pos += substr_length + 1;
                        ind3x++;
                }
        if (ind3x < array_length)
                str_array_in[ind3x] = str_in.substr(pos,str_in.size());	//what rests after the last separator
}


editedstring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef EDITEDSTRING_H
#define EDITEDSTRING_H

#include <string>
using namespace std;

class EditedString : public string
{
public:
    // Constructors
    EditedString();
    EditedString(const char* ch) : string(ch) {}
    EditedString(const string& str) : string(str) {}
    virtual ~EditedString(){};

    //Operator"="
    EditedString& operator=(const string& str) {return (EditedString&)this->assign(str);}
    EditedString& operator=(const char* ch) {return (EditedString&)this->assign(ch);}
}

#endif // EDITEDSTRING_H 


editedstring.cpp
1
2
3
4
5
#include "editedstring.h"

EditedString::EditedString()
{
}


what am I doing wrong?
(I searched google, but couldn't find what I am doing wrong)
Put a semi-colon at the end of the class definition in editedstring.h (train of logical thought here, so that next time you can solve it yourself - line 6 is fine, so the problem must be before line 6, what's before line 6? The header file editedstring.h - let's look at that, from the end - oh look, no semi-colon at the end of a class definition).

As an aside, it's considered bad manners to put using namespace std; in a header in such a way that it will cause anything including that header file to also be using namepace std (not so important in a small set of code that only you will use, but if you do it in shared code that others will have to use, you will not be popular).
Last edited on
hm, I feel pretty stupid right now ;)

Thanks a lot!

(I will follow your advise)
Topic archived. No new replies allowed.