I keep getting this error on a sample that a c++ book provides.

"

 **error C2660: 'std::vector<_Ty>::empty' : function does not take 1 arguments
    1>          with
    1>          [
    1>              _Ty=std::string
    1> **         ]

"
I have some suspicious what can be causing the error but I am not sure.
the code is:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> inventory;
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");

    cout << "You have " << inventory.size() << " items.\n";
    cout << "\nYour items:\n";
    for (unsigned int i = 0; i < inventory.size(); ++i)
    {
        cout << inventory[i] << endl;
    }

    cout << "\nYou trade your sword for a battle axe.";
    inventory[0] = "battle axe";
    cout << "\nYour items:\n";
    for (unsigned int i = 0; i < inventory.size(); ++i)
    {
        cout << inventory[i] << endl;
    }

    cout << "\nThe item name '" << inventory[0] << "' has ";
    cout << inventory[0].size() << " letters in it.\n";

    cout << "\nYour shield is destroyed in a fierce battle.";
    inventory.pop_back();
    cout << "\nYour items:\n";
    for (unsigned int i = 0; i < inventory.size(); ++i)
    {
        cout << inventory[i] << endl;
    }

    cout << "\nYou were robbed of all of your possessions by a thief.";
    inventory.clear();
    if (inventory.empty())
    {
        cout << "\nYou have nothing.\n";
    }
    else
    {
        cout << "\nYou have at least one item.\n";
    }

    return 0;
}

Dani AI

Generated

Short summary for : the snippet shown in the first post is correct and will compile (as noted). The Visual C++ error C2660 means the compiler saw a call to std::vector<T>::empty with an argument — 's observation that the member takes no parameters is the immediate clue. This usually means the source the compiler is actually compiling is different from the sample, or a name/macro collision is changing what the compiler sees.

Useful checks to find the culprit:

  • Search the entire project for calls to .empty( (look for any call that passes an argument).
  • Search headers for a #define empty or other macros that could be interfering.
  • Verify the exact file being built (stale object files or a different source file with the same name can cause confusion).
  • On MSVC, produce preprocessed output to reveal macro expansions.

Example commands to run in the project root:

# Unix / Git Bash / WSL
grep -nR "\.empty\s*\(" .

# PowerShell (Windows)
Get-ChildItem -Recurse -Include *.cpp,*.h | Select-String -Pattern "\.empty\s*\(" -List

And to locate problematic macro defines:

grep -nR "#define\s\+empty\b" .

A quick fix is to remove the extraneous argument so the container method is invoked with no parameters; if the intent was to check whether an element string is empty, call empty() on that string element or test the container size. If a macro or an unfortunate header causes the collision, rename the macro or adjust include order and then do a full clean rebuild so no stale objects remain. The grep/preprocessor checks will point directly to the offending line for correction.

Recommended Answers

All 2 Replies

That code compiles without any problems. This suggests that the code you showed us is not the code you're trying to compile.

Hey, the error is

vector.empty() doesn't take 1 argument

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.