By Geralt_of_Rivia


2020-08-01 13:42:00 8 Comments

#include <iostream>
using namespace std;

int *i = new int;
*i = 0;
int &j = *i;
j++;

//cout << *i << endl;

I have a code like that, and I know this syntax is true but it gives warning in Visual Studio Code like this:

quiz2_q8.cpp:5:4: error: expected constructor, destructor, or type conversion before '=' token
 *i = 0;
    ^
quiz2_q8.cpp:7:1: error: 'j' does not name a type
 j++;

Am I missing a library to include? I thought iostream is enough for this quiz code.

3 comments

@Pawan Nirpal 2020-08-01 14:17:12

Most programs have a starting point, which is the main method/function/procedure whatever you want to call it. Each function has a scope given by { // fun scope }. A good tutorial series on C++ might come to your aid, or perhaps a book. With that said here's a template for such a program.

#include <iostream>
using namespace std;

int main(){
   
    
   return 0;
}

@cigien 2020-08-01 13:44:26

You can't have arbitrary statements in the global namespace. You need to put it into a function, e.g. like this:

int main() {
  int *i = new int;
  *i = 0;
  int &j = *i;
  j++;
}

@MikeCAT 2020-08-01 13:43:53

Statements for being executed must be inside functions.

#include <iostream>
using namespace std;

int main(void) { // add this

    int *i = new int;
    *i = 0;
    int &j = *i;
    j++;

    //cout << *i << endl;

} // add this

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] What is a smart pointer and when should I use one?

1 Answered Questions

[SOLVED] /usr/bin/locale: source file is not valid UTF-8

  • 2020-06-28 02:35:59
  • Lin Weiye
  • 49 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ macos g++

1 Answered Questions

25 Answered Questions

[SOLVED] error C2065: 'cout' : undeclared identifier

19 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 533873 View
  • 1845 Score
  • 19 Answer
  • Tags:   c++ linux profiling

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

9 Answered Questions

8 Answered Questions

2 Answered Questions

Sponsored Content