-1

Class of leetcode question.Product of array except self.It is telling Line 6: Char 20: error: expected unqualified-id vector new;.How to fix it.

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
     int i,j; 
      int result=1;
       vector<int> new; 
    
     for(i=0;i<nums.size();i++)
     {
          result=1;
         for(j=0;j<nums.size();j++)
         {
             if(j!=i)
                result=result*nums[j];
                 
         }
        new[i]=result;   
         
     }
         
     return new;   
        
        
    
    }
};
  • 1
    [`new`](https://en.cppreference.com/w/cpp/language/new) is a [keyword](https://en.cppreference.com/w/cpp/keyword) in C++. – Ch3steR Jan 04 '22 at 04:54
  • 1
    It might be silly for any C++ programmer but someone who is `new` to C++, may struggle with the error message and it was a valid question so there was no need to downvote it. By the way as others suggested, you can simply call it `newV` and the code will compile. – Ashutosh Raghuwanshi Jan 04 '22 at 05:29
  • 1
    Note that leetcode is a collection of C++ code examples of extremely bad quality maintained by people who have nothing to do with professional programming. Don't use that website to learn C++, prefer a [good introductory book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Evg Jan 04 '22 at 06:41

1 Answers1

2

new is a keyword. Keywords cannot be used as names of variables.

How to fix it.

Pick another name that isn't a keyword, or otherwise reserved.

eerorika
  • 232,697
  • 12
  • 197
  • 326