Hello

I have an array in a function that I am trying to copy into a private array named pic_links although I am getting an error, it wont let me copy the array into another array.

Do you know why? :)

Error message:

no matching function for call to `strcpy(std::string[299], std::string[299])'

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>

using namespace std;

class generate_post {
public:
	void read(char* filename, int& counter);
	void random(int array[], int max);
	void create_post( int& counter);
	void record_used_pics();

private:
	string pic_links[299];
	string used_pic[299];
	string post[6];
	int random_p[6];
};

int main() {

	int pic_count = 0;

	generate_post html;

	html.read("pic_links.txt",pic_count);
	html.create_post(pic_count);
	html.record_used_pics();

	return 0;

}

void generate_post::read(char* filename, int& counter) {

	char x;
	string pic_array[299];
	ifstream infile;

	infile.open(filename);

	if (!infile) {
		cout << "Failed to open file";
	}


	while (infile >> x) {
		if (x=='m') {
			getline(infile,pic_array[counter],'\n');
			counter++;
		}
	}

	infile.close();

	counter = counter-1;

	strcpy(pic_array,pic_links); // error occurs here
}

Try private: vector<string> varible_name; Vector is dynamic and you can kill off unused strings, unlike a constant array.


Oh yeah, std::string is different from a C-string(char pointers/arrays) don't mix them up.

Oh yeah, std::string is different from a C-string(char pointers/arrays) don't mix them up.

Thanks, so if I make pic_links a c string then strcpy will work? :)

Is strcpy the correct function to copy the contents of one array into another? Or should I be using a different function? :)

I changed pic_links to a vector & now I am trying to copy the contents of pic_array into pic_links but I am getting an error again?

error:

no match for call to `(std::vector<std::string, std::allocator<std::string> >) (std::vector<std::string, std::allocator<std::string> >&)'

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

class generate_post {
public:
	void read(char* filename, int& counter);
	void random(int array[], int max);
	void create_post( int& counter);
	void record_used_pics();

private:
	vector<string> pic_links;
	vector<string> used_pic;
	string post[6];
	int random_p[6];
};

int main() {

	int pic_count = 0;

	generate_post html;

	html.read("pic_links.txt",pic_count);
	html.create_post(pic_count);
	html.record_used_pics();

	return 0;

}

void generate_post::read(char* filename, int& counter) {

	char x;
	vector<string> pic_array;
	ifstream infile;

	infile.open(filename);

	if (!infile) {
		cout << "Failed to open file";
	}


	while (infile >> x) {
		if (x=='m') {
			getline(infile,pic_array[counter],'\n');
			counter++;
		}
	}

	infile.close();

	counter = counter-1;

	pic_links(pic_array);  //error occurs here
}

just make your own strcpy. Its not hard

//copy string 2 into string 1
 bool stringCopy(std::string* str1, std::string* str2)
{
   if(!str2[0]) return false;

  for(int i = 0; i < str2.size; i++)
       str1[i] = str2[i]; 

  return true;
}
string temp;
...
    getline(infile, temp);
    pic_links.push_back(temp);
...

Most of your code is unnecessary, since most of it is already provided.

just make your own strcpy. Its not hard

//copy string 2 into string 1
 bool stringCopy(std::string* str1, std::string* str2)
{
   if(!str2[0]) return false;

  for(int i = 0; i < str2.size; i++)
       str1[i] = str2[i]; 

  return true;
}

Why not just str1 = str2;

"Why not just str1 = str2;"

Just an example of the logic involved to create strcpy. Its there
so he could use a similar one for which he needs, need it not to
be a char*.

When working with std::string only, I recommend to use the equal sign to copy one string to another. Usually I would use strcpy only when I'm caught in a situation dealing with char array and std::string. Is this the right way of using strcpy? Can anyone give some good advice?


Regards,
Nick

You shouldn't be calling strcpy() AT ALL in a C++ program using std::string to store strings.

The only time you need to do it is for the case where you're calling an historic interface that requires a "char*" pointer. Historic interfaces which accept "const char*" are not a problem.

Eg.

void hist1( const char *p );
void hist2( char *p );
...
string mystring = "hello";

// can be called directly using the c_str() result
hist1( mystring.c_str() );

// need to make a temp copy, just in case hist2() decides to
// modify the string.
char *temp = new char[mystring.length()+1];
strcpy( temp, mystring.c_str() );
hist2( temp );
// mystring = temp; // if you're interested in the result.
delete [] temp;

Samlem is right, forget about strcpy, go with the more same
std::string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.