C++ program unknown error

Dear Experts,

I'm learning c++ from a book. It has a exercise which is giving error

Eclipse c++ compiler
Error: strcpy was not declared in this scope
Error: strcat was not declared in this scope

While I have already included required library 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
/*============================================================================
 Write a program that overloads arithmetic addition operator + for concatenating
 two staring values.
 /============================================================================*/
#include <iostream>
#include <conio.h>
#include <string>
#include <stdio.h>
using namespace std;

class String {
private:
	char str[50];
public:
	String() {
		str[0] = '\0';
	}
	void in() {
		cout << "Enter string:";
		gets(str);
	}
	void show() {
		cout << str << endl;
	}
	String operator +(String s) {
		String temp;
		strcpy(temp.str, str);
		strcat(temp.str, s.str);
		return temp;
	}
};

int main() {
	String s1, s2, s3;
	s1.in();
	s2.in();
	cout << "s1 = ";
	s1.show();
	cout << "s2 = ";
	s2.show();
	cout << "s3 = ";
	s3.show();
	cout << "Concatenating s1 and s2 in s3..." << endl;
	s3 = s1 + s2;
	cout << "s3 = ";
	s3.show();
}


It would be much appreciate If I know why it is happening.

Regards
Adeel
What book are you learning from?
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
#include <iostream>
#include <conio.h>
#include <cstring> 
#include <stdio.h>
using namespace std;

class String {
private:
	char str[50];
public:
	String() {
		str[0] = '\0';
	}
	void in() {
		cout << "Enter string:";
		gets(str);
	}
	void show() {
		cout << str << endl;
	}
	String operator +(String s) {
		String temp;
		strcpy(temp.str, str);  //这个函数的头文件要用到cstring 
		strcat(temp.str, s.str);//同上 
		return temp;
	}
};

int main() {
	String s1, s2, s3;
	s1.in();
	s2.in();
	cout << "s1 = ";
	s1.show();
	cout << "s2 = ";
	s2.show();
	cout << "s3 = ";
	s3.show();
	cout << "Concatenating s1 and s2 in s3..." << endl;
	s3 = s1 + s2;
	cout << "s3 = ";
	s3.show();
}
Error: strcpy was not declared in this scope
Error: strcat was not declared in this scope

That's because you didn't include the <cstring> header file.

Also you should never use gets() this function can never be used safely, so forget you ever heard about it. You should be using the C++ streams instead of the C stdio functions.

Topic archived. No new replies allowed.