sparky2012
sparky2012

Reputation: 55

Calling a member Function (size) of a Referenced Vector Throwing Error

I'm trying to pass a vector to a function (printGrid) and have it print out the size of the vector. The vector is being passed by reference. When I try to compile, I get the error:

std::vector<char,std::allocator<char>>::size': non-standard syntax; use '&' to create a pointer to member

This is the relevant part of the program in question:

void printGrid(const std::vector<char>& grid, int width) { 
    std::cout << grid.size;
}

int main() {

    int width, height;
    std::cin >> width >> height;

    std::vector<char> grid = getGrid(width, height);

    printGrid(grid, width);

    return 1;
}

Upvotes: 0

Views: 467

Answers (1)

Carlton
Carlton

Reputation: 4297

You need to put parentheses after the size function, e.g.:

cout << grid.size();

Otherwise the compiler thinks you're trying to access a member variable named "size" instead of the member function of the same name.

Upvotes: 3

Related Questions