compiler doing problms

why my compiler dont know the strcpy.
always makes me trouble when iam using this method of strcpy
how to avoid it?

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
#include<iostream>
#include<cstring>
#include<cassert>
using namespace std;

class str {
private:
	char *ptr;
public:
	str();
	str(const char *);
	str(const str&);
	~str();
	friend ostream &operator<<(ostream &out, const str &o) {
		out << o.ptr << endl;
		return out;
	}
	
};
str::str() {
	ptr = NULL;
}
str::str(const char *s) {
	ptr = new char[strlen(s) + 1];
	assert(ptr != NULL);
	strcpy(ptr, s);
}
str::str(const str &copy) {
	ptr = new char[strlen(copy.ptr) + 1];
	assert(ptr != NULL);
	strcpy(ptr, copy.ptr);

}
str::~str() {
	delete[] ptr;
}

int main() {
	str b("aaaa");
	cout << b;
	system("pause");
	return 0;



}
Very strange that your compiler knows strlen but not strcpy.
What compiler do you use ?
You also could write your own copy function.
iam using visual studio 2017
what do you mean write your own copy function?(copy constractur?)
oh to copy it manuel you mean?
Last edited on
what do you mean write your own copy function?

Have a look at this old C style code I wrote for a friend.
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
#include <stdio.h> 
#include <string.h>
#include <assert.h>

char * StrCpy(char * dest, const char * source);

int main(void)
{
  char msg[] = "Hello World";
  char dest[12] = {0};

  StrCpy(dest, msg);
  printf("Dest = %s", dest);
}

char * StrCpy(char * dest, const char * source)
{
  int i;

  assert(dest != NULL);
  assert(source != NULL);
  assert(strlen(source) > 0);
  
  for (i = 0; source[i] != '\0'; i++)
    dest[i] = source[i];

  dest[i] = '\0';

  return dest;
}
thanks thomas mate (:
i already done my own copy function.
why my compiler dont know the strcpy.
always makes me trouble when iam using this method of strcpy

Don’t you think ‘trouble’ is a bit vague as an error description?

Could you please give the following code a chance?
If it doesn’t work, could you please detail what errors do you meet?


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
#include<cstring>
#include<cassert>
#include<iostream>
#include <limits>

using namespace std;

class str {
private:
    char *ptr;
public:
    str();
    str(const char *);
    str(const str&);
    ~str();
    friend ostream &operator<<(ostream &out, const str &o) {
        out << o.ptr << endl;
        return out;
    }
    
};

str::str() {
    ptr = NULL;
}

str::str(const char *s) {
    ptr = new char(strlen(s) + 1);
    assert(ptr != NULL);
    strcpy(ptr, s);
}

str::str(const str &copy) {
    ptr = new char(strlen(copy.ptr) + 1);
    assert(ptr != NULL);
    strcpy(ptr, copy.ptr);

}

str::~str() {
    delete ptr;
}

void waitForEnter();

int main() {
    str b("aaaa");
    cout << b;
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Very strange. Can you post the exact error message that you're getting?
> why my compiler dont know the strcpy.
> always makes me trouble when iam using this method of strcpy

If the 'trouble' is that it generates a warning when we use std::strcpy(),
this particular trouble may be blithely ignored.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstring>
#include <iostream>

int main()
{
    char cstr[128] ;
    
    // the microsoft compiler, by default, would give you a warning for this standard function.
    // this warning may safely be completely ignored
    // 
    std::strcpy( cstr, "hello world!\n" ) ;
    // microsoft nonsense:  
    // *** warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. 
    //                    To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    //                    note: see declaration of 'strcpy'
    
    std::cout << cstr ;
}

http://rextester.com/HCDQH47050

std::strcpy() is the standard function; it is not deprecated. ::strcpy_s() is a non-standard, non-portable extension that some library implementations provide. Any function, including the ::strcpy_s() that Microsoft trumpets, is unsafe when used in an unsafe manner.

If this warning distresses you, it can be suppressed by defining the preprocessor symbol _CRT_SECURE_NO_WARNINGS (before any header is parsed).
1
2
3
4
5
6
7
8
9
10
11
12
13
#define _CRT_SECURE_NO_WARNINGS // ask microsoft to stop complaining about 
                                // standard functions that are not deprecated 
#include <cstring>
#include <iostream>

int main()
{
    char cstr[128] ;
    
    std::strcpy( cstr, "hello world!\n" ) ;
    
    std::cout << cstr ;
}

http://rextester.com/ZLF30829

As a general rule, use standard functions whenever they are available.
It is ok to write your own version of strcpy as a purely academic exercise; once you have learned something from it, throw the home-grown version away; use std::strcpy()
Topic archived. No new replies allowed.