0

getting this error expected a declaration. using vscode to solve leetcode problems

#include <iostream>

#include <vector>
using std::vector;

class solution{

public:;

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

    { 

        unordered_map<int, int> _map;

        for(int i =0; i < nums.size(); i++){

            int num = nums[i];

            int complement = target - num;

            auto it = _map.find(complement);

            if(it != _map.end()){

                return {it->second, i};

            }

            _map[num] = i;

        }return {}
    }


};

error:

"message": "expected a declaration",
"source": "C/C++",
"startLineNumber": 8,
"startColumn": 4,
"endLineNumber": 8,
"endColumn": 5
kuro
  • 3,214
  • 3
  • 15
  • 31
Kri
  • 1
  • 2
  • 1
    `int twoSum( ...` not ` twoSum( ...`. There seem to be other problems with the code as well. – john Jul 26 '22 at 12:01
  • 2
    Your C++ syntax is completely wrong... start from the basis with a good C++ book – Marco Beninca Jul 26 '22 at 12:02
  • 2
    We can add `public:` not `public:;` – john Jul 26 '22 at 12:03
  • 2
    Really however you are trying to learn C++, it's not very *efficient*. These are bizarre mistakes which imply that you not getting your information from any reliable source and are just winging it instead. It's really not a good strategy for learning C++. – john Jul 26 '22 at 12:05
  • It looks like you had `public: vector` and then some keyboard-related accident replaced "vector" with ";". – molbdnilo Jul 26 '22 at 12:15
  • You are also missing at least one header. – molbdnilo Jul 26 '22 at 12:17
  • [Some books you can read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – limserhane Jul 26 '22 at 12:29

1 Answers1

0

You probably want this:

...
#include <vector>
#include <unordered_map>
...
using namespace std;

class solution {

public:
      vector<int> twoSum(vector <int>& nums, int target)
      {
        unordered_map<int, int> _map;

        for (int i = 0; i < nums.size(); i++) {
          int num = nums[i];
          int complement = target - num;
          auto it = _map.find(complement);

          if (it != _map.end()) {
            return { it->second, i };
          }

          _map[num] = i;
        }
        return {};
      }
};

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115