Never had this error before...

This is the error:

1
2
3
4
5
6
7
8
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 7.11 [1]\vervang.h|4|error: variable or field 'vervang' declared void|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 7.11 [1]\vervang.h|4|error: 'string' was not declared in this scope|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 7.11 [1]\vervang.h|4|error: 's' was not declared in this scope|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 7.11 [1]\vervang.h|4|error: expected primary-expression before 'char'|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 7.11 [1]\vervang.h|4|error: expected primary-expression before 'char'|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 7.11 [1]\main.cpp||In function 'int main()':|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Boek Opgave 7.11 [1]\main.cpp|17|error: expected ';' before 'vervang'|
||=== Build finished: 6 errors, 0 warnings ===|


and this is the code:


int main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "vervang.h"

using namespace std;

int main()
{
    string s;
    char bron, doel;

    cout<<"Give up a sentence/word: ";
    getline(cin, s);
    cout<<"Which character has to be replaced?\n";
    cin>>bron;
    cout<<"By which character?\n";
    cin>>doel
    vervang(s, bron, doel);


}


vervang.h
1
2
3
4
5
6
#ifndef VERVANG_H_INCLUDED
#define VERVANG_H_INCLUDED

void vervang(string& s, char bron, char doel);

#endif // VERVANG_H_INCLUDED 


vervang.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>

using namespace std;

void vervang (string& s,char bron,char doel)
{
    string::iterator letter = s.begin();

    for (; letter != s.end(); ++letter )
    {
        if (*letter == bron)
        *letter = doel;
    }


}
Have you tried adding <string> header to your code?
nvm, FeZedra catched it.
As mtweeman said, I had to include string and also code using namespace std!
To elaborate on the problem:

You're #including vervang.h before you say you're using namespace std, therefore when the compiler sees the 'string' in vervang.h, it doesn't know you mean std::string.

Possible solutions (listed in order of "better" to "worse") are:

1) Change vervang.h to the below:

1
2
3
4
5
6
#ifndef VERVANG_H_INCLUDED
#define VERVANG_H_INCLUDED

void vervang(std::string& s, char bron, char doel);  // <- std::string

#endif // VERVANG_H_INCLUDED  



2) put 'using namespace std' before '#include "vervang.h"' in your cpp file(s)

3) put 'using std::string' or 'using namespace std;' in vervang.h. Note I mention this for completeness only. This is actually a bad idea since it destroys the namespace, and I do not recommend it!
Oh i see, thanks a lot!
Topic archived. No new replies allowed.