error in eclipse c ++?

1

When I create a hello world it works for me but if I paste a block of code from another site I get that error.

link

this is my code:

 #include <iostream> #include <cstring> using namespace std; struct Node{ char *name; int age; Node(char *n = "", int a = 0){ name = new char[strlen(n) + 1]; strcpy(name, n); age = a; } }; Node node1("Roger", 20), node2(node1); cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age; strcpy(node2.name, "Wendy"); node2.name = 30; cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;

and this error in console:

    20:13:22 **** Incremental Build of configuration Debug for project gfr ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\gfr.o" "..\src\gfr.cpp" 
..\src\gfr.cpp:1:21: warning: extra tokens at end of #include directive
 #include <iostream> #include <cstring> using namespace std; struct Node{ char *name; int age; Node(char *n = "", int a = 0){ name = new char[strlen(n) + 1]; strcpy(name, n); age = a; } }; Node node1("Roger", 20), node2(node1); cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age; strcpy(node2.name, "Wendy"); node2.name = 30; cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
                     ^
g++ -o gfr.exe "src\gfr.o" 
c:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to 'WinMain@16'
collect2.exe: error: ld returned 1 exit status

20:13:26 Build Finished (took 4s.313ms)
    
asked by jony alton 15.04.2016 в 01:08
source

2 answers

4

The error is mainly that it is not indented correctly, if you put everything in a single line it will take part of the code as a preprocessor directive:

#include <iostream> #include <cstring> using namespace std; struct Node{ char *name; int age; Node(char *n = "", int a = 0){ name = new char[strlen(n) + 1]; strcpy(name, n); age = a; } }; Node node1("Roger", 20), node2(node1); cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age; strcpy(node2.name, "Wendy"); node2.name = 30; cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;

you must indent your code!

You also do not have a main () defined, which is extremely important! Your correct and well-formatted code should look similar to:

#include <iostream>
#include <cstring>
using namespace std;

struct Node{
    char *name;
    int age;
    Node(char *n = "", int a = 0){
        name = new char[strlen(n) + 1];
        strcpy(name, n);
        age = a;
    }
};


int main() {
    Node node1("Roger", 20), node2(node1);
    cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
    strcpy(node2.name, "Wendy");
    node2.name = 30;
    cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
}
    
answered by 15.04.2016 в 01:23
0

The compilable C ++ code has finalizing elements, namely: ; to end each instruction and } to finish functions, classes (actually these end with }; ), name spaces and execution areas. Due to this feature all the C ++ compiler code can be put in a single line and without spaces and the result will compile without problems.

The preprocessor directives do not have finalizers, so when you include a directive in the code, it will be valid until the line where the directive is located ends. Due to this restriction, only one directive per line can be included.

With all this, the problem in your case is that you are mixing preprocessor directives and executable code on the same line and the result is not legal within the scope of C ++.

Another aspect that will give you problems is that the executable code must necessarily be found (except in the case of global variables) within a function. In addition, every executable must have a main function. You do not fulfill any of these two conditions and the code, no matter how much you separate it in several lines, will not compile.

The most direct solution is then to isolate the preprocessor directives in separate lines and include the body of the code in a function main :

// Directivas del preprocesador
#include <iostream>
#include <cstring>

// Código compilable
using namespace std; struct Node{ char *name; int age; Node(char *n = "", int a = 0){ name = new char[strlen(n) + 1]; strcpy(name, n); age = a; } }; int main(){ Node node1("Roger", 20), node2(node1); cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age; strcpy(node2.name, "Wendy"); node2.name = 30; cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age; }

Although as you mentioned in other answers, the most advisable is to structure the code in several lines and with indentations to make it readable:

#include <iostream>
#include <cstring>

using namespace std;

struct Node{
  char *name;
  int age;

  Node(char *n = "", int a = 0)
  {
    name = new char[strlen(n) + 1];
    strcpy(name, n);
    age = a;
  }
};

int main()
{
  Node node1("Roger", 20), node2(node1);
  cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
  strcpy(node2.name, "Wendy");
  node2.name = 30;
  cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
}
    
answered by 07.02.2017 в 10:16