Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Vectors

Name: Anonymous 2011-05-25 8:55

I got a function that adds to the vector which is

scores = new vector<string>;

void addToVec(string name, int score)
{
    scores->push_back(name);
    scores->push_back((char*)score);
    addToFile(scores);
}

then i want to add it to a output file with

void addToFile(vector<string> scores)
{
    ofstream outFile(SCORES_FILE, ios::app);
   
    if(outFile.fail()) {
        cout << "Could not write to scores file, exiting program" << endl;
        exit(1);
    }
   
    for (int i = 0; i < scores.size(); i++) {
        outFile << scores[i];
        outFile << " ";
    }
   
    outFile.close();
}

But i keep getting a "no matching function for the call 'addToFile(std::basic_string...etc etc"

My code all wrong?

Name: Anonymous 2011-05-26 6:45

>>4,6
Woah. WOAH. WOAH.

You sir are doing everything absolutely, completely wrong. The optimizer [b][u]☢ DOES NOT ☢[u][/b] magically make your poorly designed programs run faster, because the compiler can't tell if what you're trying to do was intentional. You are trying to program C++ as if it were Java or C#. This is why I think people should not learn Java or C# as their first language because it gives them VERY BAD habits. You should learn C and an assembly language before delving into C++, so you know how to write efficient C++ code If there was a license required to use C++, you would fail the test. Wait! You would be thrown in jail for using C++ without a license and being a danger to other C++ programmers.

Firstly, DO NOT allocate everything on the global  heap/freestore if you don't have to. This isn't Java/C#. Allocating on the heap when you don't need to is wasteful and slow in more than one way. I have NO idea why you are allocating the scores std::vector object, for example, on the heap when you can quite easily not do so, and have a more efficient program.

Next, do not do "using namespace std". NEVER. That's the sign of a complete amateur. And anyone who says otherwise cannot be trusted. ALWAYS fully qualify Standard Library types and functions with std:: namespace prefix. You will thank me later!

Next, passing objects around in C++ by value induces these side-effects known as copy constructors and assignment operators. For things like ints and floats, they are known as trivial copy constructors and get optimized out of the compiler even in debug mode. But for other library types like std::string and std::vector, every time you pass by value, you invoke the copy constructor and it makes a completely new copy. Instead, you should pass by (const) reference. If you don't, the compiler thinks you actually did want to invoke the copy constructor and won't optimize it away.

Example:

void Class::addToVec(std::string const& name, int score) {
}


Finally, you need to learn how to use code tags when posting your code. This is extremely important!

Follow the above rules, and you will find the road to satori much easier!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List