Dynamic string arrays in function parameters

D

Daniel Fudge

I can't send a string from a dymanic array to a function.

At the getpts function declarationon (line 14), I get the following
error.

"main.cpp(14) : error C2664: 'void __thiscall
std::basic_ifstream<char,struct std::char_traits<char> >::eek:pen(const
char *,int)' : cannot convert parameter 1 from 'class
std::basic_string<
char,struct std::char_traits<char>,class std::allocator<char> >' to
'const char *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called"

I have something like:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

main() {
int n = 2;
std::string *filenames = new std::string[ns];

filenames[0] = "name1";
filenames[1] = "name2";

getpts(filenames);

delete[] filenames;
}

void getpts(std::string *filenames) {
cout << filenames[0];
}

Any help would be greatly appreciated.
 
D

Donovan Rebbechi

Yes you can. You're misinterpreting the error message. If you change
int n = 2;

to
int ns = 2;

0iThen the code will compile.
"main.cpp(14) : error C2664: 'void __thiscall
std::basic_ifstream<char,struct std::char_traits<char> >::eek:pen(const
char *,int)' : cannot convert parameter 1 from 'class
std::basic_string<
char,struct std::char_traits<char>,class std::allocator<char> >' to
'const char *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called"

I think you're getting the error because you are writing code that looks
something like this:

ifstream in;
in.open(filenames[0]);

open requires an argument of const char*, so you need
in.open(filenames[0].c_str());
I have something like:

[snip]

"Something like" isn't good enough, you should try to post an example that
reproduces the error message you report (the code I snipped doesn't, it
produces an unrelated error message because you mistyped the identifier for the
array length)

BTW, may I suggest using vector<string> instead of a dynamic array ?

Cheers,
 
K

Kevin Goodsell

Daniel said:
I can't send a string from a dymanic array to a function.

At the getpts function declarationon (line 14), I get the following
error.

"main.cpp(14) : error C2664: 'void __thiscall
std::basic_ifstream<char,struct std::char_traits<char> >::eek:pen(const
char *,int)' : cannot convert parameter 1 from 'class
std::basic_string<
char,struct std::char_traits<char>,class std::allocator<char> >' to
'const char *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called"

I don't believe you got this error from this code, mainly because this
error refers to ifstream::eek:pen(), and you aren't using ifstream anywhere.

However, this error is usually caused by something like this:

std::string filename = "somefile.txt";
std::ifstream fin;
fin.open(filename); // This is an error!

ifstream::eek:pen() takes a const char * as its first argument, not a
std::string. Change the last line above to

fin.open(filename.c_str());

to fix the problem.
I have something like:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

You aren't using fstream or sstream in the code that follows.
using namespace std;

main() {

You forgot to specify a return type for main. This is required in C++.
If your implementation doesn't complain, it's broken.

It's also required that the return type for main() be 'int'.
int n = 2;
std::string *filenames = new std::string[ns];

You don't need the std:: qualifiers here, since you imported the entire
namespace. It would be better to get rid of the 'using namespace std'
line and keep the explicit qualifications, though.
filenames[0] = "name1";
filenames[1] = "name2";

getpts(filenames);

This function has not been declared. You must declare it before calling it.
delete[] filenames;
}

void getpts(std::string *filenames) {
cout << filenames[0];
}

Any help would be greatly appreciated.

-Kevin
 
G

Gianni Mariani

Daniel said:
I can't send a string from a dymanic array to a function.

At the getpts function declarationon (line 14), I get the following
error.

"main.cpp(14) : error C2664: 'void __thiscall
std::basic_ifstream<char,struct std::char_traits<char> >::eek:pen(const
char *,int)' : cannot convert parameter 1 from 'class
std::basic_string<
char,struct std::char_traits<char>,class std::allocator<char> >' to
'const char *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called"

I have something like:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

main() {

int main()
int n = 2;
std::string *filenames = new std::string[ns];

..... [n] (not [ns])
filenames[0] = "name1";
filenames[1] = "name2";

getpts(filenames);

undefined method - needs a declaration before this point
delete[] filenames;
}

void getpts(std::string *filenames) {
cout << filenames[0];
}

move this function above main
Any help would be greatly appreciated.

Hope this helps.
 
D

Default User

Daniel said:
I have something like:


When your car has a problem, do you take in car that looks kind of like
yours?

Show us the exact code that causes the problem. Cut and paste right out
of your development environment if you can. First trim it down to the
smallest subset that demonstrates your problem (sometimes you find the
error on your own this way).




Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top