0

EDIT

*Please , upvote this topic because I cant ask anymore question in this forum. Programming is my life and I'm just stuck because of an automated banning . Thank you ( or I need the help of a moderator to solve this problem *

I'm a beginner programmer in c++ and I want to basically return a std::vector When I debug my code I get function call missing argument list . Here is my simple code

Thanks for your help

#include "stdafx.h"
#include <vector>
#include <iostream>

static std::vector<int> returnStaticVector();

static std::vector<int> returnStaticVector(){
    std::vector<int> vectorInt = std::vector<int>();
    vectorInt.push_back(0);
    return vectorInt;

}

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> a = std::vector<int>();

    a = returnStaticVector(); // Compile , but error when I try to access to the size of the std::vector

    //int size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member  
    //int size = &a.size; // & Illegal operation
    //int& size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member 
    int* size = a.size; //error C3867: 'std::vector<int,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<int,std::allocator<_Ty>>::size' to create a pointer to member   

    return 0;
}
sachaamm
  • 139
  • 2
  • 13

2 Answers2

9

In std::vector size is a member function, not a member variable. You use it like this:

int size = a.size();

If you don't have the parentheses, it is a syntax error.

Incidentally, another thing you can do to simplify your code is declare vectors like this:

std::vector<int> a;

Or in C++11

std::vector<int> a{};

Both of these will default-construct the vector -- this works for any class type.

Doing it this way,

std::vector<int> a = std::vector<int>();

is not as good because it is longer and makes you type things twice, and also it copy initializes it rather than default constructing it, which is slightly different and may be less efficient.

Chris Beck
  • 15,614
  • 4
  • 51
  • 87
  • I want to ask you , is it a efficient way to return std::vector instead of pointing them ? the fact is I don't know the size of elements i'm gonna get so I can't use a simple pointer with new – sachaamm Jan 13 '16 at 00:29
  • 4
    @sachaamm One question per question please, comments are for critique or request for clarification. I would tell you to post a new question, but what you ask has been answered before on this site. Use the search function. – Baum mit Augen Jan 13 '16 at 00:33
1

First things first -- The real compile problem in your code is because you have used a.size instead of a.size(). Try changing that and the code should compile successfully.

Apart from that, I don't think it's a good idea to return a vector like what you have done. Try passing vectors using reference from the calling function. That is much better design.

If you are still thinking about return by value think in terms of Copy elision which is an optimization implemented by compilers to prevent unnecessary copies in many cases. It makes returning by value or pass-by-value feasible in many cases.

Read more about copy elisions here :-

http://en.cppreference.com/w/cpp/language/copy_elision

What are copy elision and return value optimization?

Community
  • 1
  • 1
Laks
  • 19
  • 4
  • 2
    He's not returning a static vector. His function is poorly named. – Benjamin Lindley Jan 13 '16 at 00:32
  • It's not a static vector, and whether passing by reference instead of returning a value is necessarily a better design is questionable. Compilers are very good at removing unnecessary copies. – molbdnilo Jan 13 '16 at 00:35
  • Highly questionable indeed. Doesn't make much sense to pass an argument unless the incoming value has some effect on the function. – Benjamin Lindley Jan 13 '16 at 00:57