Class Syntax

Hi,
pulling my hair out trying to see what is wrong with the following files. Just trying to get used to visual C++ .

The erorr message is "1str.h(8) : error C2146: syntax error : missing ';' before identifier 's'";

Any ideas?

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "str.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

int main(void){

str myS;

}

str.h
1
2
3
4
5
6
7
8
9
10
11
#pragma once
#include <string>

class str
{
public:
str(void);
string s(void);
	 
};

str.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "str.h"
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

str::str(void)
{
std::cout<<"got constructor"<<std::endl;
}

string  str::s(void){
string s2="a";
return s2;
}

 


1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include <string>

using namespace std; //this will fix the problem but including 'using' keyword in .h file is bad
                                    //according to "them".

class str
{
    public:
        str(void);
        string s(void);
};
It is bad, in a header. It forces the users of your class to accept the namespace qualifier. That's why you should fully qualify all spaces and scopes rather than performing a global using directive.
thanks for both replies!
1
2
3
4
5
6
7
8
9
#pragma once
#include <string>

class str
{
    public:
        str(void);
       std::string s(void); //oops this fixed your problem
};


std::string s(void) <---
On a side note:

You don't need to put 'void' in the parenthesis for functions that take no parameters. You can leave the parenthesis empty.

The void thing is a weird C thing.
Topic archived. No new replies allowed.