Assigning to a string from a function call

Hi,

I'm stuck on something that's probably really simple.

I've a function that returns a string value but when I try to assign it to a string variable it won't compile with

No matching function for call to std::basic_string<char>::assign(s_remaining)'
ms.assign(s_remaining(tm_end));


This is the function
1
2
3
4
5
6
7
8
9
10
11
12
13
 std::string s_remaining(tm enddate)
{
    long l_working;
     std::string msg;
     std::time_t now = time(0);
     std::time_t ending = std::mktime(enddate);

     l_working = (ending - now);
     msg = std::to_string(l_working);

     return msg;
            
}


and this is the calling code that it's failing on

1
2
3
    time_t now = time(0);
    std::string ms;
    ms.assign(s_remaining(tm_end));


As I say, I'm sure this is fairly straightforward but I've got hold the wrong end of a stick somewhere but I've spent a few days on this and have tried quite a number of different permutations in the calling statement

Cheers

Samop
What is the tm_end?
Whoops, should have included that.

This is the complete "relevant" bit of the calling code up to the error
1
2
3
4
5
6
7
8
9
10
11
12
void MainWindow::setup()
{

    static struct tm tm_end;

    tm_end.tm_year = 2014;
    tm_end.tm_mon = 06;
    tm_end.tm_mday = 10;
   // time_t now = time(0);

    std::string ms;
    ms.assign(s_remaining(tm_end));


Samop
How about ms = s_remaining(tm_end);

How does the compilation unit containing the MainWindow::setup() do know about the function s_remaining? Same file or declared in header and implemented in another translation unit?
Hi

ms = s_remaining(tm_end); was my first attempt and that didn't work.

The function s_remaining is declared in a header file
1
2
3
4
5
class s_remaining
{
public:
     s_remaining(tm);
};


and it's implemented in another .cpp file which contains all my "utilities" and stuff

Is the header declaration OK?

Oh my,

Given
1
2
3
4
5
class s_remaining
{
public:
     s_remaining(tm);
};

your code:
s_remaining(tm_end);
does actually mean:
Create an object of type class s_remaining. Initialize the object with 'tm_end' object.
This would require the class s_remaining to have a contructor that accepts struct tm as parameter.

It is not a "function call".

If you want to call that non-static member function, you have to have an object:
1
2
3
std::string ms;
s_remaining foo;
ms = foo.s_remaining( tm_end );
Oh, how embarrassing (but I did say "I'm stuck on something that's probably really simple" and ".... I'm sure this is fairly straightforward but I've got hold the wrong end of a stick somewhere"). That's why I posted this on the beginners forum.

But thanks for that Keskiverto. I've re-read the chapters on class definitions etc and can see that I totally missed the point.

My justification here is that I've been a VB programmer for many years and I now realise that in that language everything that's even slightly "tricky" is hidden/automated so you don't have to think. e.g. I've been involved in a couple of seriously major projects (thousands of classes) and never needed to do an overload.

I'm teaching myself C++ as a private "hobby" to stretch my mind. Quite a bit more stretching needed.

Cheers

Samop
Registered users can post here. Sign in or register to post.