LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2014, 12:30 PM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Error while returning string


I written a method whatTime() as shown below

Code:
class Time {
public:
        string whatTime(int);
};

string Time::whatTime(int seconds) {
string result = "";
int hours,minutes,secs;
hours = seconds /3600;
minutes = (seconds % 3600) / 60;
secs = seconds % 60;
result = hours + ":" + minutes + ":" + secs;
return result;
}
When i compile the code, getting error

test.cpp:33:34: error: invalid operands of types ‘const char*’ and ‘const char [2]’ to binary ‘operator+’

I am new to C++ programming.
 
Old 04-24-2014, 12:48 PM   #2
bcwagne
Member
 
Registered: Feb 2008
Distribution: Debian Testing
Posts: 169

Rep: Reputation: 32
Can you let us know line numbers of your code? Or let us know which lines are throwing the error?
 
Old 04-24-2014, 12:56 PM   #3
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
Code:
result = hours + ":" + minutes + ":" + secs;
is giving error

Last edited by srinietrx; 04-24-2014 at 01:13 PM.
 
Old 04-24-2014, 01:52 PM   #4
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
Your main problem is that you're trying to form a string by concatenating integers and (C) strings, which is not what you want to do. You should probably be using a stringstream for things like this - have a look at cppreference.com (or any other C++ reference docs). I'm not allowed to post links, but this info is easy enough to find.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dpkg returning an error code (1) ???????? william11 Linux - General 11 08-14-2011 12:18 PM
Problem in returning String from thread in java BlueSkull Programming 2 11-22-2009 10:44 AM
c++ problem setting "const char *str" from function returning a string ocularb0b Programming 3 03-04-2008 09:36 PM
startx returning an error lg_alucard Slackware 5 01-10-2005 05:17 PM
Error returning - t: NT_STATUS_OK May1950 Linux - Newbie 1 04-23-2004 02:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration