error: invalid operands of types ‘const char*’ and ‘const char [4]’ to binary ‘operator+’
Mathias Panzenböck
grosser.meister.morti at gmx.net
Fri Jul 1 02:50:40 CEST 2011
I would not build the string like that at all. Also I don't think a + operator for int and
std::string is defined (which would be needed for the track).
Use e.g.:
std::stringstream ss;
ss << "INSERT INTO SongList (track, name, genre, artist, album, time, uri) values (" <<
tag->track() << ", '" << tag->title() << "', '" << tag->genre() + "', '" <<
tag->artist() << "', '" << tag->album() << "', '" << minutes << ":" <<
seconds << "', '" << *iter << "');";
std::string insert = ss.str();
But then you should not build an SQL query like that anyway because of possible injection attacks.
Or just plain buggy behaviour (what if the title or artist contains a ' character? it's not that
uncommon). Isn't there an API somethink like this for the SQL lib you use?
Query insert("INSERT INTO SongList (track, name, genre, artist, album, time, uri) "
"values (?, ?, ?, ?, ?, ?, ?);");
insert
.addParameter(tag->track())
.addParameter(tag->title())
.addParameter(tag->genre())
.addParameter(tag->artist())
.addParameter(tag->album())
.addParameter(minutes)
.addParameter(*iter);
connection.execute(insert);
-panzi
On 07/01/2011 02:26 AM, Phong Cao wrote:
> Hello everybody,
>
> I am trying to use TagLib to extract tags from a stream and store it in a std::string. Here is the
> function I used:
>
> void TopWin::insert_tag_on_db(std::vector<std::string> &filenames) {
> for (std::vector<std::string>::iterator iter = filenames.begin(); iter != filenames.end(); ++iter) {
> TagLib::FileRef file((*iter).c_str());
> if (!file.isNull() && file.tag()) {
> TagLib::Tag *tag = file.tag();
> TagLib::AudioProperties *properties = file.audioProperties();
> int seconds = properties->length() % 60;
> int minutes = (properties->length() - seconds) / 60;
> //error occurs here
> std::string insert = "INSERT INTO SongList (track, name, genre, artist, album, time, uri)
> values (" + tag->track() + ", '" + tag->title() + "', '" + tag->genre() + "', '" + tag->artist() +
> "', '" + tag->album() + "', '" + minutes + ":" + seconds + "', '" + *iter + "');";
> }
> }
> }
>
> However, the program was not compiled. g++ reported an error:
>
> /home/phongcao/dingo/topwin.cc:16: error: invalid operands of types ‘const char*’ and ‘const char
> [4]’ to binary ‘operator+’
>
> I have read the TagLib documentation and it said that the TagLib's return type is TagLib::String.
> What I do not understand here is whether TagLib::String is similar to std::string or char*? If it is
> similar to std::string then what is the source of the above error?
>
> I guess the source of the error is that I was trying to concatenate tag->title(), tag->track(),
> etc... (which may have types char*, since I do not know TagLib::String is a CString or a
> std::string) to a string literal. Can you guys suggest a solution to fix this error?
>
>
> Thank you for reading my message! Have a good day!
>
>
> Best regards,
>
> --
> Phong V. Cao
> phngcv at gmail.com <mailto:phngcv at gmail.com>
> caoph at rider.edu <mailto:caoph at rider.edu>
>
>
>
>
> _______________________________________________
> taglib-devel mailing list
> taglib-devel at kde.org
> https://mail.kde.org/mailman/listinfo/taglib-devel
More information about the taglib-devel
mailing list