I am a very new beginner so please excuse my ignorance.
I literally started learning c++ 3 days ago.

I am attempting to read a file and split each line into 2 arrays.
The first half the line is just text and i would like to store it for re-output in an of array.

The arrays has to vary in size depending on information passed to the program.

string **name;
	name = new string*[atoms];
	for (int i=0;i<atoms;i++){
		name[i]=new string[8];

while (getline(coordsfile,line)){
	size_t i = line.find("\t");
	if (i != string::npos){
		size_t y=0;
		if (!line.empty()){
			string first="";
			string second="";
			while (y!=i){
				first += line[y++];
			name[y] = first;

When I try to compile I get the following error.
rotation.cpp(97): error: no suitable conversion function from "std::string" to "std::string *" exists.

What is the differnence between these data types?
How can I make the conversion?

Please let me know if you need any more of my code. I tired to keep it uncluttered.

Have you heard of pointers? (clicky)

You are trying to convert directly into a pointer. Because of what pointers are and how they work, you can not convert anything directly into a pointer.

Take one '*' out of line 1 and eliminate the '*' from line 2. I think you're doing something weird on lines 3-5 as well, but I'm not entirely sure. Can you elaborate on your intent?

Edited 6 Years Ago by Fbody: n/a

lines 3-5 were there because I was attempting to initialize the array the same way I had for my 2-d arrays later on. ( I guessed that strings were just char arrays and would need similar initialization)

After removing the * as suggested I just get seg faults.
I realize that segfault is a bit more difficult to diagnose
if i comment out just the assignment line it works fine (line 18)

I don't see anything related to this in my messages
icc -Wall rotation.cpp only shows messages from later in the code

Edited 6 Years Ago by spikeru: n/a

An std::string is based on a char array, but they are not equivalent datatypes.

string *name;  //creates a pointer to an std::string
	name = new string[atoms];  //expands the pointer to an array of (atoms) pointers to strings
	for (int i=0;i<atoms;i++){
	    name[i]=new string[8];
            //you are trying to create a new array dimension that contains an incompatible type
            //you need to assign literals to the dereferenced pointer(s)
            //i.e. (*arrayName[element]) = "a literal"

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.