1

I just want to do some initial checks, and have some quick of {-1, -1} vector return if necessary. Somehow, compiler says: I should change return type into vector *

But the current return type works for my later parts before I do this pre-checks code. So what do I misunderstood?

class SomeClass {
  public:
    static vector<int> solution(vector<int>& numbers, int target) {
      if (numbers.empty() || numbers.size() < 2) {
        return new vector<int> {-1, -1};   // <== Compile Error
      }
      unordered_map<int, int> hash;
      vector<int> result;
      .
      .
      .
      return result;
    }
};

int main() {
  vector<int> testNums = {11, 15, 2, 7};
  vector<int> result = SomeClass::solution(testNums, 9);
  return 0;
}
2
  • 4
    remove keyword new Commented Jul 16, 2018 at 3:58
  • 2
    And vector <int> - the compiler will figure that bit out for itself. Commented Jul 16, 2018 at 4:46

1 Answer 1

5

Somehow, compiler says: I should change return type into vector *

In C++, you need a pointer (or a pointer-like type) to point dynamically allocated memory.

Thus, replacing:

return new vector<int> {-1, -1};   

with:

return { -1, -1 };

will make it work as you'd expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.