Home > Mobile >  error: variable or field ‘MetroHastings’ declared void
error: variable or field ‘MetroHastings’ declared void

Time:11-07

I'm new to StackOverflow and quite new to C . I've a problem when I try to define a function inside my program "ising.cpp". That's the body function, it isn't complete yet but its development is unrelated to my error:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include <stdlib.h>
#include "libreria.h"      

using namespace std;


void MetroHastings (system * old_state,int method) {
system new_state;
new_state = *old_state;
}


int main () {

return 0;

}

I think the problem it's connected to the class system costruction, which is inside "liberia.h":

#ifndef libreria_h
#define libreria_h



using namespace std;

struct vecI_2d {

int nx;
int ny;

};

struct vecD_2d {
   double x;
   double y;

};

struct atom {

  double spin; // 0,1,-1
};


class system {
   double T;
   int i,j;
   double energy;
   double J = 1;
   atom ** particles;   
   public:
   system();    
   system (double T, int ix,int iy);
    void number_particle (int n);
    void ComputeEnergy();
    double ReturnEnergy();
    double CloseEnergy(int ix,int iy);
    double magnetization();
    };

    #endif

And the class body definitions are in "liberia.cc":

     #include "libreria.h"      
     #include <iostream>
     #include <cstdlib>
     #include <cmath>
     #include <time.h>
     #include <stdlib.h>

     using namespace std;

     system::system(double T, int sx, int sy) {
     i=sx;
     j=sy;
     int r;
     particles = new atom *[i];
     for (int k=0;k<i;k  ) {
        particles[k] = new atom[j];
      }
       for (int kx=0;kx<i;kx  ) { 
         for(int ky=0;ky<j;ky  ) {
            r = rand()%1;
            if (r==1) {
                particles[kx][ky].spin = 1;
            }
            else {
                particles[kx][ky].spin = -1;
            } 
        }
      }
   }

etc... That's the command I used to compile:

g   ising.cpp libreria.cc -o ising

I don't understand why I get that error. I always defined functions inside my cpp file, I don't know why the compiler mistakes it for a variable declaration. Thank you in advance :)

CodePudding user response:

Your class named system conflicts with the standard function with the same name.

Clang emits a much better error message:

<source>:47:21: error: must use 'class' tag to refer to type 'system' in this scope
void MetroHastings (system * old_state,int method) {
                    ^
                    class 
.../stdlib.h:78:12: note: class 'system' is hidden by a non-type declaration of 'system' here
using std::system;
           ^

Rename the class, or use class system instead of system to refer to it, as suggested by Clang.


For anyone wondering, removing using namespace std; doesn't help here, and neither does replacing <stdlib.h> with <cstdlib>.

CodePudding user response:

When a class and a function have the same name then the function hides the class declaration.

Your class name system conflicts with the standard C function system that hides the class definition.

From the C 14 Standard (3.3.1 Declarative regions and scopes)

4 Given a set of declarations in a single declarative region, each of which specifies the same unqualified name

(4.2) — exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden (3.3.10). [ Note: A namespace name or a class template name must be unique in its declarative region (7.3.2, Clause 14). — end note ]

In such a case you need to use the elaborated class specifier like

void MetroHastings ( class system * old_state,int method) {
    system new_state;
    new_state = *old_state;
}

Also instead C header names you need to use C header names like for example

#include <cstdlib>

CodePudding user response:

firstly, there's already a definition/declaration of 'system' in one of the previous header (stdlib.h) files, i suggest you resolve that first. Its also good form to show all the outputs, helps folks give better advice.

  • Related