【问题标题】:error: variable or field ‘MetroHastings’ declared void错误:变量或字段“MetroHastings”声明为无效
【发布时间】:2021-12-20 14:34:08
【问题描述】:

我是 StackOverflow 的新手,对 C++ 还是很陌生。当我尝试在我的程序“ising.cpp”中定义一个函数时,我遇到了问题。那是body函数,它还没有完成,但它的开发与我的错误无关:

#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;

}

我认为它与 class system 构造有关,它位于“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

并且类主体定义在“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;
            } 
        }
      }
   }

等等... 这是我用来编译的命令:

g++ ising.cpp libreria.cc -o ising

我不明白为什么会出现这个错误。我总是在我的 cpp 文件中定义函数,我不知道为什么编译器会将其误认为是变量声明。提前谢谢你:)

【问题讨论】:

  • 这里问的时候尽量减少代码。这个问题可以用三行代码复现:#include &lt;stdlib.h&gt;class system {};void foo(system *bar) {}
  • 首先,在之前的一个头文件(stdlib.h)中已经有一个“系统”的定义/声明,我建议你先解决这个问题。它也是显示所有输出的好形式,可以帮助人们提供更好的建议。

标签: c++ scope declaration unqualified-name


【解决方案1】:

您的名为 system 的类与 standard function with the same name 冲突。

Clang 发出更好的错误消息:

<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;
           ^

重命名类,或使用class system 而不是system 来引用它,如 Clang 所建议的那样。


对于任何想知道的人来说,删除 using namespace std; 在这里没有帮助,将 &lt;stdlib.h&gt; 替换为 &lt;cstdlib&gt; 也没有帮助。

【讨论】:

  • 谢谢!你们都非常有帮助!
【解决方案2】:

当类和函数同名时,函数会隐藏类声明。

您的类名 system 与隐藏类定义的标准 C 函数 system 冲突。

来自 C++ 14 标准(3.3.1 声明性区域和范围)

4 给定单个声明区域中的一组声明,每个声明 它指定了相同的非限定名称

(4.2)——只有一个声明应该声明一个类名或 不是 typedef 名称的枚举名称和其他声明 都应指同一个变量或枚举数,或都指 函数和函数模板; 在这种情况下是类名或 枚举名称是隐藏的 (3.3.10)。 [注意:命名空间名称或 类模板名称在其声明区域中必须是唯一的(7.3.2, 第 14 条)。 ——尾注]

在这种情况下,您需要使用详细的类说明符,例如

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

此外,您还需要使用 C++ 标头名称来代替 C 标头名称,例如

#include <cstdlib>

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    相关资源
    最近更新 更多