Hi everybody I am working on a String project for my class and I'm stuck on this swap function. This is what my professor has in his "tests" :

void test_swap_first_shorter()
{
    // Setup fixture
    string foo("foo");
    string barbar("barbar");

    // Test
    foo.swap(barbar);

    // Verify
    assert(foo == "barbar");
    assert(barbar == "foo");
    assert(foo.size()    == 6);
    assert(barbar.size() == 3);
}

and this is what I have in mine.

void string::swap(string first)
    {   
        string second;

        first.swap(second);
    }

His way of assigning the projects are a little weird so I really don't understand what he's asking us to do here. Oh and when I go to run his make file, it slows down my computer so it's definitely doing something wrong. Thanks in advance.

Well you are infinitely running the loop. with your function call.

string foo("foo");
    string barbar("barbar");

With the above code, Your Professor is assigning the values foo and barbar to the variables through a constructor.

void string::swap(string first)
    {   
        string second;

        first.swap(second);
    }

With this code you are causing infinite calls maybe due to which your computer does nothing...........

Normally you can swap

swap(var1,var2)
{
new var3;

var3=var1;
var1=var2
var2=var3
}

With that explaination set. Why dont you try to swap

I think the (*this) command will be useful.

Thanks Sky Diploma is this what you mean:

void string::swap(string first)
    {   
        string temp, second;
        temp = first; // or this temp = *this
        first = second;
        second = temp;
 }

Am I on the right track or no?

void string::swap(string& first)

That would get you on the right track. As you will need to change the value of the first variable too.

I would'nt give everything up. So that would just be a start. I guess..
Try using the commented section in different ways.

void string::swap(string& first)
    {   
        string temp, second;
        *this = temp;
        first = second;
        second = *this;
    }

Is this any closer? Would it be easier to understand if I understood his tests?

Hey you seem to make a mistake.

(*this) represents the original variable which is "foo" in your professors code.
the variable "first" represents the second variable which is "barber" in your professors code.

So you decide how to exchange values by making a temp variable ;)

void string::swap(string& first)
    {   
        string temp, second;//There is no need for second here.
        *this = temp;//You are assigning temp to foo which doesnt make sense because temp doesnt have a value
        first = second;
        second = *this;
    }
void string::swap(string& first)
    {   
        string temp;
        temp = *this; 
        *this = first; // can I do this?
        first = temp;
    }

That's what i have now. I'm making this harder than it really is huh? haha

void string::swap(string& first)
    {   
        string temp;
        temp = *this; 
        *this = first; // can I do this?
        first = temp;
    }

That's what i have now. I'm making this harder than it really is huh? haha

void string::swap(string& first)
    {   
        string temp;
        temp = *this; 
        (*this) = first; // With the "(" and ")" added i guess it should copy value like pointers do
        first = temp;
    }

Try it out now .

Well it all compiles it's just not passing his tests. A friend just told me there needs to be something else done with the project, I'm sure I will post again though. Thanks for your time I appreciate it.

>>// With the "(" and ")" added i guess it should copy value like pointers do
Even if you didn't it would still be same.

To the OP:
The "Test" of you professor is perhaps not good. The interface given by him is already implemented as std::swap
What sky diploma told you must be true. That is, you should create a function, take two strings( by reference) and swap them(as he told you in post #2).

The "Test" of you professor is perhaps not good. ... What sky diploma told you must be true..

i dont know, you may be right.... BUT

it's generally a Bad Idea for a first year programming student to tell the professor the fundamental premise of his assignment is flawed.

unless that student is prepared to deliver a bulletproof example and orally defend it against close scrutiny.

at this level, given the OP is still asking basics, its probably best to fit the answer to the question, regardless of whether the question is technically precise.


.

i dont know, you may be right.... BUT

it's generally a Bad Idea for a first year programming student to tell the professor the fundamental premise of his assignment is flawed.

unless that student is prepared to deliver a bulletproof example and orally defend it against close scrutiny.


.

LOL, I am not telling him to shut the professors mouth but informing him.
But look at the interface. If the professor meant that this interface should be implementation for the std::string, it is a tough assignment for the beginner.
It may be possible that profesor has given the OP to design a 'home made' string class which should have a member called swap(), then it makes sense and OP is actually on the right track.

It may be possible that profesor has given the OP to design a 'home made' string class which should have a member called swap(), then it makes sense and OP is actually on the right track.

and that underscores my point that you and i have no context to tell some student that his professor is full of beans.

putting those ideas in some beginning students head will only serve to get them embarrassed, or worse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.