473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dynamic string arrays in function parameters

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> >::open(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.
Jul 22 '05 #1
4 3076
Daniel Fudge wrote:
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> >::open(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::open(), 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::open() 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2
Daniel Fudge wrote:
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> >::open(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.

Jul 22 '05 #3
In article <e9**************************@posting.google.com >, Daniel Fudge wrote:

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> >::open(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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #4
Daniel Fudge wrote:
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Thomas Hede Jensen | last post by:
Hi, I'm currently working on a project in which I link to a library where the function definitions takes parameters of the type: "const int *const *paramName" As far as I can tell from the...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
3
by: repairman2003 | last post by:
I'm having some trouble with poitners and dynamic arrays (a matrix). Given this function I have a few questions. void func(int* mat, int rows, int columns, char* out) { ... }
2
by: DaveL | last post by:
Hello I want to build Dynamic Paramers for a Sql Insert below is what i have so far but...determinthe column type and length im having Problems with Tks dave string sInsert = "Insert into...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.