我只是偶然发现使用Mozilla Rhino作为JavaScript引擎的奇怪的JavaScript错误.
这一行脚本抛出一个错误:
eval("const a = 5;");
Run Code Online (Sandbox Code Playgroud)
错误是:
TypeError: redeclaration of var a.
Run Code Online (Sandbox Code Playgroud)
我希望这个错误,如果该行多次执行 - 但它只执行一次,因为这一行是整个程序.
谁能解释一下,为什么会出现这个错误?
可以在包含声明命名空间的命名空间中定义命名空间成员:
通过定义名称的显式限定(3.4.3.2),也可以在该命名空间外定义命名空间的成员,前提是已定义的实体已在命名空间中声明,并且定义出现在命名空间中的声明点之后包含声明的命名空间.
void f();
namespace N { void ::f() {} } // illegal for definition
namespace N { void ::f(); } // what about redeclaration?
Run Code Online (Sandbox Code Playgroud)
可以在包含声明命名空间的命名空间中定义类:
如果class-head-name包含嵌套名称说明符,则类说明符应引用先前在嵌套名称说明符所引用的类或命名空间中直接声明的类,或者在该命名空间的内联命名空间集(7.3.1)(即,不仅仅是由using-declaration继承或引入),并且类说明符应出现在包含前一个声明的命名空间中.在这种情况下,定义的类头名的嵌套名称说明符不应以decltype-specifier开头.
struct A;
namespace N { struct ::A {}; } // illegal for definition
namespace N { struct ::A; } // what about redeclaration?
Run Code Online (Sandbox Code Playgroud)
我们对成员函数定义和静态数据成员定义也有相同的规则.
所以我的问题是重新声明(不是定义)在没有包含原始声明的命名空间中是否合法?
我一直在与这个错误作斗争.错误是在函数中的某个地方我现在有php告诉我它不能在同一行重新声明变量...奇怪.任何帮助都会很棒.
致命错误:无法重新声明/ home/bp-member-login-redirect/bp中的bp_block_admin_init()(之前在/home/bp-member-login-redirect/bp-member-login-redirect-loader.php:31中声明)第31行的-member-login-redirect-loader.php
第29-31行
// make sure buddypress is installed
function bp_block_admin_init() {
require_once( dirname( __FILE__ ) . '/bp-member-login-redirect-core.php' );}
Run Code Online (Sandbox Code Playgroud)
实际调用函数的行(我在代码中得到错误,有或没有这些行:
if (defined( 'BP_VERSION' ))
bp_block_admin_init();
else
add_action( 'bp_init', 'bp_block_admin_init' );
Run Code Online (Sandbox Code Playgroud) class me {
private $name;
public function __construct($name) { $this->name = $name; }
public function work() {
return "You are working as ". $this->name;
}
public static function work() {
return "You are working anonymously";
}
}
$new = new me();
me::work();
Run Code Online (Sandbox Code Playgroud)
致命错误:无法重新声明我:: work()
问题是,为什么php不允许这样重新声明.有没有解决方法?
我刚刚完成了我的应用程序并且它工作正常,但是在按下文件 -> 复制后突然间,我有一条消息说
'ViewController' 的重新声明无效
如果可以的话请帮帮我好吗?谢谢
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet var slideScrollView: UIScrollView!
@IBOutlet var pageControl: UIPageControl!
override func viewDidLoad() {
slideScrollView.delegate = self
let slides:[Slide] = createSlides()
setupSlideScrollView(slides: slides)
pageControl.numberOfPages = slides.count
pageControl.currentPage = 0
view.bringSubview(toFront: pageControl)
}
func createSlides () -> [Slide] {
let slide1: Slide = Bundle.main.loadNibNamed("Slide", owner:self, options:nil)?.first as!Slide
slide1.label.text = "Slide1"
let slide2: Slide = Bundle.main.loadNibNamed("Slide", owner:self, options:nil)?.first as!Slide
slide2.label.text = "Slide2"
return [slide1, slide2]
}
func setupSlideScrollView (slides:[Slide]) {
slideScrollView.frame …Run Code Online (Sandbox Code Playgroud) 据我所知,在C++中,只要在所有这些声明中具有相同的类型,就可以多次声明相同的名称.要声明类型的对象int,但不定义它,extern则使用关键字.所以以下内容应该是正确的并且编译没有错误:
extern int x;
extern int x; // OK, still declares the same object with the same type.
int x = 5; // Definition (with initialization) and declaration in the same
// time, because every definition is also a declaration.
Run Code Online (Sandbox Code Playgroud)
但是一旦我将它移到函数内部,编译器(GCC 4.3.4)就会抱怨我正在重新声明x并且它是非法的.错误消息如下:
test.cc:9: error: declaration of 'int x'
test.cc:8: error: conflicts with previous declaration 'int x'
Run Code Online (Sandbox Code Playgroud)
int x = 5;第9行在哪里,extern int x在第8行.
我的问题是:
如果多个声明不应该是错误,那么为什么在这种特殊情况下它是一个错误?
我对全局常量有点困惑.我(初学者级别)的理解是'全局'变量是在块之外定义的并且具有程序范围(来源:http://www.learncpp.com/cpp-tutorial/42-global-variables/).但该计划:
#include <iostream>
const double x=1.5;
int main(){
std::cout << "1) x=" << x << std::endl;
double x=2.5;
std::cout << "2) x=" << x << std::endl;
//const double x=3.5;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用-Wall编译g ++(GCC,最新的64位版本),没有任何问题.
输出:
1) x=1.5
2) x=2.5
Run Code Online (Sandbox Code Playgroud)
这对我来说很困惑.第一个cout评估的事实意味着main将'x'识别为'全局'变量(它未在main的范围中定义).如果是这样的话,为什么让我重新定义'x'?
然后,如果取消注释注释的第三个声明,g ++会引发重新声明错误.意思是,我的第一个宣言不能是'全球',在我定义的意义上:S
编辑:好的,问题与全局变量无关,但是范围:例如http://pastebin.com/raw.php?i=V5xni19M中的相同问题
我的 C 项目中出现编译错误。我有一个包含此枚举的头文件:
typedef enum {
RAD_ALLOWED= 0,
RAD_STOPPED ,
RAD_OFF
} Values_E;
Run Code Online (Sandbox Code Playgroud)
并在另一个带有此枚举的头文件中:
typedef enum {
RAD_ALLOWED= 0,
RAD_STOPPED ,
RAD_OFF
} Values_X;
Run Code Online (Sandbox Code Playgroud)
当我将两个头文件包含在同一个 c 文件中时,我遇到类似于以下内容的错误:
214: error: previous definition of 'RAD_STOPPED ' was here
129: error: redeclaration of enumerator 'RAD_STOPPED '
Run Code Online (Sandbox Code Playgroud)
是的,两个枚举的内容是相同的,但名称不同,所以我不明白为什么会出现这个问题。请注意,包含这些枚举的两个头文件都是自动生成的,因此我无法更改它们的内容。
具有以下for循环:
for (var i = 0; i < 3; ++i) {
console.log(i, p);
var p;
p = 42;
}
Run Code Online (Sandbox Code Playgroud)
我期待输出为:
0 undefined
0 undefined
0 undefined
Run Code Online (Sandbox Code Playgroud)
但实际上,输出是:
0 undefined
0 42
0 42
Run Code Online (Sandbox Code Playgroud)
由于我们正在使用var p(重新声明p)下一行,为什么行中没有p未定义(总是)console.log?
我需要siginfo_t在标头中声明不完整的类型(可能是),并在源导入中sys/signal.h定义该类型的一些系统标头()。
所以,问题来了:
// a.h
struct siginfo_t;
void foo(siginfo_t*);
Run Code Online (Sandbox Code Playgroud)
// a.cpp
#include "a.h"
#include <signal.h>
void foo(siginfo_t* x) { /* implementation */ };
Run Code Online (Sandbox Code Playgroud)
但在 signal.h 中它的定义如下:
typedef struct siginfo_t { /* */ } siginfo_t;
Run Code Online (Sandbox Code Playgroud)
编译器错误:error: typedef redefinition with different types ('struct siginfo_t' vs 'siginfo_t').
如何实现没有错误呢?谢谢!
redeclaration ×10
c++ ×4
const ×2
definition ×2
javascript ×2
php ×2
buddypress ×1
c ×1
declaration ×1
enums ×1
eval ×1
for-loop ×1
function ×1
global ×1
instance ×1
ios ×1
loops ×1
namespaces ×1
scope ×1
static ×1
struct ×1
swift ×1
typedef ×1
wordpress ×1
xcode ×1