0

I'm trying to call the vector function 'twoSum' which is inside the class 'Solution' but is giving me 'Expected variable name or 'this' in lambda capture list' error when I'm trying to call the function.

Here's the code:

#include <iostream>
#include <list>
#include <vector>
using namespace std;

class Solution {
public:
        vector<int> twoSum(vector<int>& nums, int target) {
        for (int i=0; i+1<=nums.size(); i++){
            if ((nums[i] + nums[i+1]) == target){
                //cout << "[" << i << i+1 << "]";
                return (i,i+1);
            }
        }
        return {2, 3};
    }
};

int main()
{
    Solution solobject;
    solobject.twoSum([1,2],2); //Not Calling function
    return 0;
}

What am I doing wrong?

  • Rather than trying to pass a `vector` like you are, have you tried creating a local vector and passing that in? – Tas Dec 03 '20 at 05:46
  • (1) Pass the vector argument by const-ref/value or as the comment above suggests (2) `solobject.twoSum({1,2},2);`, specifically curly not square braces. – tangy Dec 03 '20 at 05:48
  • 5
    Neither method you're using to create a vector is correct. Where are you learning C++? – Stephen Newell Dec 03 '20 at 05:48
  • @Tas Do you have an example for that? – UltimateTechDiscovery Dec 03 '20 at 05:51
  • @StephenNewell So this problem is from LeetCode. I'm just including the main part so I can run it locally but is not working – UltimateTechDiscovery Dec 03 '20 at 05:52
  • 1
    If LeetCode is showing this as sample code, then I'd find another resource learn from. I'd suggest [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Stephen Newell Dec 03 '20 at 05:54
  • @tangy I added curly and I'm now getting the error: "Non-const value reference to type 'vector' cannot bind to an initializer list temporary" – UltimateTechDiscovery Dec 03 '20 at 05:54
  • @UltimateTechDiscovery that's what point (1) covered. As Stephen mentioned though, it might be better to pick up a better learning resource first. To better understand (1) you can try this SO answer - https://stackoverflow.com/q/15600499/3656081 – tangy Dec 03 '20 at 05:59

1 Answers1

2

In C++, you can't create a std::vector with a [1, 2] expression.

The most similar thing will be implicitly creating an instance of std::vector by list initializer with {1, 2}. See more about this in std::vector and std::initializer_list documentation.

Also, there will be an issue with passing to the function. Because right now your signature accepts a non-const lvalue reference vector<int>& nums and you are passing an rvalue instead.

You need to either make your method to accept a value like this:

vector<int> twoSum(vector<int> nums, int target)

or accept an rvalue:

vector<int> twoSum(vector<int>&& nums, int target)

or create an instance of the vector in main() before calling the method.

Also, as mentioned by @Stephen Newell, you may want to make it a const reference, because we don't change the std::vector in the method:

int main()
{
    Solution solobject;
    auto vec = std::vector<int>({1,2})
    solobject.twoSum(vec,2); //Not Calling function
    return 0;
}

And as mentioned by @tangy, we can't create a std::vector with (i,i+1), we need to use the same initialiser list with {i, i + 1}.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Valerii Boldakov
  • 1,751
  • 9
  • 19