C++ HelloWorld with header

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

#include <string>


class Stringer{

	public:
		Stringer();
		~Stringer();
		st
			ring getHelloWorld();
		void setHellowWorld(string);
		
	private:
		string helloWorld;

}

#endif //hw1_h 


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
60
61
62
63
64
65
/* hw1.cpp
   This is a hello world

   Author: Jarryd Goodman
   Date Created: January 17, 2013

*/

#include "hw1.h"
#include <stdio.h>
#include <string.h>


Stringer::Stringer(){
// Initializes class
//
// input: none
// output:none
//

	helloWorld = "Hello World";

}

Stringer::setHelloWorld(string inVal){
// Sets attribute hello world 
//
// input: string value
// output:none
//

	helloWorld = inVal;

}

Stringer::getHelloWorld(){
// Returns hello world
//
// input: none
// output: string value
//

	return helloWorld;

}

// MAIN FUNCTION

using namespace std;

int main(int argc, chard ** argv){

	Stringer hello;
	string writeOut;

	hello.setHelloWorld("Hello World!");

	writeOut = hello.getHelloWorld();

	cout << "Here is my string: " << endl;
	cout  << writeOut << endl;

	exit(0);

}


I'm getting a whole list of errors that I do not understand, can anybody help me so I can get this to build?

1>------ Build started: Project: Hw1, Configuration: Debug Win32 ------
1> hw1.cpp
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C2146: syntax error : missing ';' before identifier 'getHelloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): warning C4183: 'getHelloWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(14): error C2061: syntax error : identifier 'string'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C2146: syntax error : missing ';' before identifier 'helloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(14): error C2533: 'Stringer::{ctor}' : constructors not allowed a return type
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(21): error C2065: 'helloWorld' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2039: 'setHelloWorld' : is not a member of 'Stringer'
1> c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(8) : see declaration of 'Stringer'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2065: 'string' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2146: syntax error : missing ')' before identifier 'inVal'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2059: syntax error : ')'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2143: syntax error : missing ';' before '{'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(43): error C2065: 'helloWorld' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(51): error C2061: syntax error : identifier 'chard'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(53): error C2264: 'Stringer::Stringer' : error in function definition or declaration; function not called
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(56): error C2039: 'setHelloWorld' : is not a member of 'Stringer'
1> c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(8) : see declaration of 'Stringer'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(60): error C2065: 'cout' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(61): error C2065: 'cout' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The line 13 error is easy to solve: put the 'ring' part on the next line directly after the 'st' part.

Put a semicolon after the closing curly brace of the class declaration in hw1.h.

In hw1.cpp, put the returntype of the member functions before their names:
void Stringer::setHelloWorld(string inVal){

#include <string.h> should be #include <string>

If you haven't put using namespace std; in one of your files, use the std:: prefix on string and cout in that file.
I fixed line 13, put returntypes in front of member functions, and I did have using namespace std right before main at the end of hw1.cpp. I ended up with a bunch more errors.



1>------ Build started: Project: Hw1, Configuration: Debug Win32 ------
1> hw1.cpp
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C2146: syntax error : missing ';' before identifier 'getHelloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): warning C4183: 'getHelloWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(14): error C2061: syntax error : identifier 'string'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C2146: syntax error : missing ';' before identifier 'helloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(14): error C2533: 'Stringer::{ctor}' : constructors not allowed a return type
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(21): error C2065: 'helloWorld' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2039: 'setHelloWorld' : is not a member of 'Stringer'
1> c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(8) : see declaration of 'Stringer'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2065: 'string' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2146: syntax error : missing ')' before identifier 'inVal'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2182: 'setHelloWorld' : illegal use of type 'void'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2059: syntax error : ')'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2143: syntax error : missing ';' before '{'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36): error C2143: syntax error : missing ';' before 'Stringer::getHelloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(43): error C2065: 'helloWorld' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(51): error C2061: syntax error : identifier 'chard'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(53): error C2264: 'Stringer::Stringer' : error in function definition or declaration; function not called
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2872: 'string' : ambiguous symbol
1> could be 'c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36) : int string'
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(2063) : std::string'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2872: 'string' : ambiguous symbol
1> could be 'c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36) : int string'
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(2063) : std::string'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2146: syntax error : missing ';' before identifier 'writeOut'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2065: 'writeOut' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(56): error C2039: 'setHelloWorld' : is not a member of 'Stringer'
1> c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(8) : see declaration of 'Stringer'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(58): error C2065: 'writeOut' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(60): error C2065: 'cout' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(61): error C2065: 'cout' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(61): error C2065: 'writeOut' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I used basically the same code my professor was using in class and a lot of the errors don't make sense to me (IE cout undeclared identifier). I'm starting to think that maybe the problem lies within VisualStudio. Any thoughts? I will provide the updated code and error reports.


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
60
61
62
63
64
65
/* hw1.cpp
   This is a hello world

   Author: Jarryd Goodman
   Date Created: January 17, 2013

*/

#include "hw1.h"
#include <stdio.h>
#include <string.h>


Stringer::Stringer(){
// Initializes class
//
// input: none
// output:none
//

	helloWorld = "Hello World";

}

void Stringer::setHelloWorld(string inVal){
// Sets attribute hello world 
//
// input: string value
// output:none
//

	helloWorld = inVal;

}

string Stringer::getHelloWorld(){
// Returns hello world
//
// input: none
// output: string value
//

	return helloWorld;

}

// MAIN FUNCTION

using namespace std;

int main(int argc, chard ** argv){

	Stringer hello;
	string writeOut;

	hello.setHelloWorld("Hello World!");

	writeOut = hello.getHelloWorld();

	cout << "Here is my string: " << endl;
	cout  << writeOut << endl;

	exit(0);

}


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

#include <string>


class Stringer{

	public:
		Stringer();
		~Stringer();
		string getHelloWorld();
		void setHellowWorld(string);
		
	private:
		string helloWorld;
}

#endif //hw1_h 


1>------ Build started: Project: Hw1, Configuration: Debug Win32 ------
1> hw1.cpp
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C2146: syntax error : missing ';' before identifier 'getHelloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(13): warning C4183: 'getHelloWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(14): error C2061: syntax error : identifier 'string'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C2146: syntax error : missing ';' before identifier 'helloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(14): error C2533: 'Stringer::{ctor}' : constructors not allowed a return type
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(21): error C2065: 'helloWorld' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2039: 'setHelloWorld' : is not a member of 'Stringer'
1> c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(8) : see declaration of 'Stringer'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2065: 'string' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2146: syntax error : missing ')' before identifier 'inVal'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2182: 'setHelloWorld' : illegal use of type 'void'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2059: syntax error : ')'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2143: syntax error : missing ';' before '{'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(25): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36): error C2143: syntax error : missing ';' before 'Stringer::getHelloWorld'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(43): error C2065: 'helloWorld' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(51): error C2061: syntax error : identifier 'chard'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(53): error C2264: 'Stringer::Stringer' : error in function definition or declaration; function not called
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2872: 'string' : ambiguous symbol
1> could be 'c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36) : int string'
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(2063) : std::string'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2872: 'string' : ambiguous symbol
1> could be 'c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(36) : int string'
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(2063) : std::string'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2146: syntax error : missing ';' before identifier 'writeOut'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(54): error C2065: 'writeOut' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(56): error C2039: 'setHelloWorld' : is not a member of 'Stringer'
1> c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.h(8) : see declaration of 'Stringer'
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(58): error C2065: 'writeOut' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(60): error C2065: 'cout' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(61): error C2065: 'cout' : undeclared identifier
1>c:\users\j goodies\documents\school\cis 269\hw1\hw1\hw1.cpp(61): error C2065: 'writeOut' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
At a quick glance. You're passing in to main "chard"! This should be "char".
Lynx876 is right about the char correction.

However, the reason you're getting all of these error is because you're missing a semicolon at the end of your class declaration.

Also, I'd be inclined to put the Stringer class's functions in it's own source file and keep main separate.
I see on line 15 of your header, this "void setHellowWorld(string);" and you make calls to it later as "setHelloWorld()" You have an extra w in the declaration. 1 more error down.
I get compile time errors on lines 24 & 35:
void Stringer::setHelloWorld(<error-type> inVal)

Error: decleration is incompatible with "void Stringer::setHelloWorld(<error-type> inVal)" (declared at line 24)


and line 59:
Error: identifier "cout" is undefined


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

#include <string>


class Stringer{

	public:
		Stringer();
		~Stringer();
		string getHelloWorld();
		void setHelloWorld(string);
		
	private:
		string helloWorld;
		string writeOut;
};

#endif //hw1_h 


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
60
61
62
63
64
/* hw1.cpp
   This is a hello world

   Author: Jarryd Goodman
   Date Created: January 17, 2013

*/

#include "hw1.h"
#include <stdio.h>



Stringer::Stringer(){
// Initializes class
//
// input: none
// output:none
//

	helloWorld = "Hello World";
}

void Stringer::setHelloWorld(string inVal){
// Sets attribute hello world 
//
// input: string value
// output:none
//

	helloWorld = inVal;

}

string Stringer::getHelloWorld(){
// Returns hello world
//
// input: none
// output: string value
//

	return helloWorld;

}

// MAIN FUNCTION

using namespace std;

int main(int argc, char ** argv){

	Stringer hello;
	string writeOut;

	hello.setHelloWorld("Hello World!");

	writeOut = hello.getHelloWorld();

	cout << "Here is my string: " << endl;
	cout  << writeOut << endl;

	exit(0);

}
Try putting a "using namespace std;" in your header file, and if that doesn't work then move the one in your .cpp up to just underneath your #include files.
There are more problems with your code.
It's bad practice to use using namespace std in header files becaus in can cause a lot of problems.
In the header file use std::string and std::cout where ever you use them, because the header doesn't know the string to use, like so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// hw1.h
#ifndef hw1_h
#define hw1_h

#include <string>


class Stringer{

	public:
		Stringer();
		~Stringer();
		std::string getHelloWorld();
		void setHelloWorld(std::string);
		
	private:
		std::string helloWorld;
		std::string writeOut;
};

#endif //hw1_h  


Also you don't need to include the string header again since you include it in the hw1.h header. So remove the #include<string.h> from hw1.cpp.
Also move "using namespace std;" right after the includes.
Also in the same file replace #include <stdio.h> with #include<iostream> That's anothe reason c++ doesn't recognize your cout's
Post any errors that are left.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// hw1.h
#ifndef hw1_h
#define hw1_h

#include <string>


class Stringer{

	public:
		Stringer();
		~Stringer();
		std::string getHelloWorld();
		void setHelloWorld(std::string);
		
	private:
		std::string helloWorld;
		std::string writeOut;
};

#endif //hw1_h 


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
60
61
62
63
64
/* hw1.cpp
   This is a hello world

   Author: Jarryd Goodman
   Date Created: January 17, 2013

*/

#include "hw1.h"
#include <iostream>

using namespace std;


Stringer::Stringer(){
// Initializes class
//
// input: none
// output:none
//

	helloWorld = "Hello World";
}

void Stringer::setHelloWorld(string inVal){
// Sets attribute hello world 
//
// input: string value
// output:none
//

	helloWorld = inVal;

}

std::string Stringer::getHelloWorld(){
// Returns hello world
//
// input: none
// output: string value
//

	return helloWorld;

}

// MAIN FUNCTION


int main(int argc, char ** argv){

	Stringer hello;
	string writeOut;

	hello.setHelloWorld("Hello World!");

	writeOut = hello.getHelloWorld();

	std::cout << "Here is my string: " << endl;
	std::cout  << writeOut << endl;

	exit(0);

}


Red squigly underlines (compile time errors?) are gone and I get one build error:

1>------ Build started: Project: Hw1, Configuration: Debug Win32 ------
1> hw1.cpp
1>hw1.obj : error LNK2019: unresolved external symbol "public: __thiscall Stringer::~Stringer(void)" (??1Stringer@@QAE@XZ) referenced in function _main
1>C:\Users\J Goodies\Documents\School\CIS 269\Hw1\Debug\Hw1.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And that error can be solved by putting this code in hw1.cpp:
1
2
Stringer::~Stringer() {
}
Ahhh, thank you guys! Got everything working.
Hey Nedo,
I was just curious what types of problems a using namespace could cause in a header file, I'm just curious since I've got it in my headers right now and haven't had a problem and want to know what I should look out for.

edit: I guess the main problem I can think of is if I'm working with a group where everyone has their own namespace and function names or variables were overlapped, but that wouldn't just be a problem for header files...
Last edited on
say you have a header file (stringer.h)that defines a string class of your own with your own namespace, and you have another header header2.h file in witch you add this "using namespace std;"
Header2.h is include after the stringer.h, and if you try to use your string from stringer.h you would in fact use the std string.
I will try and make an exemple when i get home of this type of problem.(Im at work now).
Please do if you have the time, trying to work that out in my head just caused me to blow a fuse. I think I get the idea behind what you're saying though.
Last edited on
Until i get home and can make an example project you can have a look at http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers on this topic. Maybe it will help you.
I don't know what's the policy about double posting so im adding my answer here instead of making a new post.
here are the codes:
stringer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef STRINGER_H_INCLUDED
#define STRINGER_H_INCLUDED

#include <string>
#include <iostream>
//#include "headers.h"

namespace stringer{
    class string
    {
    public:
        string();
        ~string();
        string(std::string str);
        string(char c[]);
        std::string getString();
    private:
        std::string m_string;
    };

}


#endif // STRINGER_H_INCLUDED 


stringer.cpp
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
#include "stringer.h"

using namespace stringer;

string::string()
{
    m_string.empty();
}

string::string(std::string str)
{
    m_string = str;
}

string::string(char c[])
{
    m_string.assign(c);
}

std::string string::getString()
{
    return m_string;
}
string::~string()
{

}


headers.h
1
2
3
4
5
6
7
8
9
#ifndef HEADERS_H_INCLUDED
#define HEADERS_H_INCLUDED
#pragma once
#include <iostream>
#include <string>

using namespace std;

#endif // HEADERS_H_INCLUDED 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stringer.h"
#include "headers.h"


int main()
{
    string std_str;
    std_str.assign("String from standard string");
    stringer::string str("String from custom string"); // this properly calls my string class from stringer
    cout << str.getString() << endl;
    cout << std_str;
    return 0;
}


Add this to a standard console project.
Supose you created your own string class like i did, and you need to use that class, but you include headers.h, then when you declare your string, you use the one from std instead of yours.
Also supose you use the stringer class in other files and you forget to add "using namespace stringer;" and add headers.h in that file. You won't be able to compile your code and might have a pretty hard time finding the error.
Try and play with this code, hope it will help you understand. If you have anymore questions just ask, i will try and help as much as i can.
Last edited on
Bump, i don't think anyone seen the answer above.
Topic archived. No new replies allowed.