我有一个问题,我认为这与前向声明有关,但也许不是.
这是相关代码:
啊
#ifndef A_H_
#define A_H_
#include "B.h"
class A
{
private:
B b;
public:
A() : b(*this) {}
void bar() {}
};
#endif /*A_H_*/
Run Code Online (Sandbox Code Playgroud)
BH
#ifndef B_H_
#define B_H_
#include "A.h"
class A;
class B
{
private:
A& a;
public:
B(A& a) : a(a) {}
void foo() { /*a.bar();*/ } //doesn't compile
};
#endif /*B_H_*/
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "A.h"
int main()
{
A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题似乎是调用A :: bar().程序成功编译,直到我尝试调用此方法,此时我得到两个错误:
错误:无效使用不完整类型'struct A'
错误:'struct A'的前向声明
我认为这是因为A :: bar()尚未定义或声明,因为两个标题相互引用.但是,我转发申报的A级,我不知道还需要做什么.我是C++的新手,所以请原谅我.我无法在网上找到这个问题的答案.一如既往,提前谢谢!
例如这是正确的:
class C
{
private:
C();
C(const & C other);
}
Run Code Online (Sandbox Code Playgroud)
或者您应该提供定义:
class C
{
private:
C() {};
C(const & C other) {};
}
Run Code Online (Sandbox Code Playgroud)
?谢谢你目前的答案.让我们扩展这个问题 - 编译器是否在其中一个示例中生成了更好的代码?我可以想象为ctor强制编译器提供体在编译单元中包含一些(空)代码?这对于自动生成的代码也是如此吗?
c++ constructor declaration copy-constructor default-constructor
使用.prototype而不是在对象本身内声明函数和成员的技术原因是什么.用代码示例解释是最容易的.
使用有什么好处:
RobsObject = function(data){
this.instanceID = data.instanceID;
this._formButton = document.getElementById('formSubmit_' + this.instanceID);
if(this._formButton)
{
//set a click listener that
//points to this._onSubmit, this._onSuccess, and this.onFailure
}
};
RobsObject.prototype = {
_onSubmit: function(type, args)
{
//make an ajax call
},
_onSuccess: function(type, args)
{
//display data on the page
},
_onFailure: function(type, args)
{
//show an alert of some kind
},
};
Run Code Online (Sandbox Code Playgroud)
反对在Object中声明你的函数,如:
RobsObject = function(data){
this.instanceID = data.instanceID;
this._formButton = document.getElementById('formSubmit_' + this.instanceID);
if(this._formButton)
{
//set a click listener …Run Code Online (Sandbox Code Playgroud) 是
int (*x)[10];
Run Code Online (Sandbox Code Playgroud)
和
int x[10];
Run Code Online (Sandbox Code Playgroud)
当量?
根据"顺时针螺旋"规则,它们解析为不同的C声明.
对于点击厌倦:
大卫安德森的"顺时针/螺旋规则"
有一种称为"顺时针/螺旋规则"的技术,可以让任何C程序员在头脑中解析任何C声明!
有三个简单的步骤:
1. Starting with the unknown element, move in a spiral/clockwise direction;
when ecountering the following elements replace them with the
corresponding english statements:
[X] or []
=> Array X size of... or Array undefined size of...
(type1, type2)
=> function passing type1 and type2 returning...
*
=> pointer(s) to...
2. Keep doing this in a spiral/clockwise direction until all tokens have been covered.
3. Always resolve …Run Code Online (Sandbox Code Playgroud) 在Fortran中,变量的声明语句是否可以引用先前声明的变量?例如,当我尝试以下操作时:
PROGRAM test3
IMPLICIT NONE
INTEGER :: a=2286
INTEGER :: b=a/3
WRITE(*,*) a, b
END PROGRAM test3
Run Code Online (Sandbox Code Playgroud)
我收到编译时错误消息:
test3.f90:5.16:
INTEGER :: b=a/3
1
Error: Parameter 'a' at (1) has not been declared or is a variable, which
does not reduce to a constant expression
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我在一个与b的声明分开的语句中将b分配给/ 2,它编译并运行正常:
PROGRAM test3
IMPLICIT NONE
INTEGER :: a=2286
INTEGER :: b
b=a/3
WRITE(*,*) a, b
END PROGRAM test3
Run Code Online (Sandbox Code Playgroud)
这给了我正确的输出:
2286 762
Run Code Online (Sandbox Code Playgroud)
为什么会这样 - 先前声明的变量不能包含在新变量的声明语句中?难道我做错了什么?或者这只是"生活中的Fortran事实"?
非常感谢您的宝贵时间!
球队,
我在哪里可以找到有关以下JavaScript代码的等式左侧的文档:
var [a, b, c] = "10-11-12".split('-');
Result:
a = 10
b = 11
c = 12
// Note: this is used in Google Apps Script
Run Code Online (Sandbox Code Playgroud)

谢谢!
我正在进行涉及使用静态变量/方法的编程任务.这是要求之一,我不确定在标题中声明它并在类文件中定义它的正确语法:
"声明类的静态方法,返回类型是对类的对象的引用;将此方法命名为"instance"."
继承我宣布的声明:
static &Singleton instance();
Run Code Online (Sandbox Code Playgroud)
继承我的定义:
static &Singleton::Singleton instance(){
static myObj;
return myObj;
}
Run Code Online (Sandbox Code Playgroud)
我不认为这是正确的..任何人都可以确认/纠正我吗?谢谢!
C++
cout构造和销毁此类消息的对象.我试图使用只有类名的声明来构造一个临时对象,但它给出了一个意外的输出.
在#1中,我使用括号实例化一个临时无名对象.
在#2中,我使用统一初始化实例化一个临时无名对象.
我不知道#3是否会编译.我只想到如果编译#3,它将表示临时无名对象的构造.它确实编译,但是从#3下控制台输出的空白处看不到构造对象.这里发生了什么?
#include <iostream>
class A
{
public:
A() {std::cout << "constructed\n";}
~A() {std::cout << "destroyed\n";}
};
auto main() -> int
{
std::cout << "#1:\n";
A();
std::cout << "#2:\n";
A{};
std::cout << "#3:\n";
A;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
#1:
constructed
destroyed
#2:
constructed
destroyed
#3:
Run Code Online (Sandbox Code Playgroud)
注意:这是在2012年11月CTP的VC11中编译的.它不能在g ++ 4.8.0或clang 3.2中编译,它们分别给出error: declaration does not declare anything [-fpermissive]和fatal error: 'iostream' file not found.
是否可以声明一个元组结构,其中除声明外,出于所有意图和目的隐藏成员?
// usize isn't public since I don't want users to manipulate it directly
struct MyStruct(usize);
// But now I can't initialize the struct using an argument to it.
let my_var = MyStruct(0xff)
// ^^^^
// How to make this work?
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以使成员保持私有状态,但仍然允许使用上面的参数初始化新结构?
作为一种替代方法,MyStruct::new可以实现诸如之类的方法,但是我仍然想知道是否有可能避免在类型上使用方法,因为它更短,并且对于包装单个变量的类型来说很好。
无需过多讨论细节,此类型的唯一目的是包装单个类型(隐藏一些细节,添加一些功能并在编译时进行完全优化的帮助器),在这种情况下,它并没有完全暴露出要使用的隐藏内部结构在Struct(value)风格初始化。此外,由于包装器的开销为零,因此使用new通常与分配/创建而不是强制转换相关联的方法会产生一些误导。
就像它是方便键入(int)v或int(v),而不是int::new(v)一样,我想针对自己的类型进行此操作。
它经常使用,因此使用短表达式的功能非常方便。当前,我正在使用一个宏,该宏调用一个new方法,它的方法还可以,但是有点尴尬/间接,因此这个问题出现了。
C11标准在第6.7/5节中规定哪些声明也是定义:
C11 6.7/5
标识符的定义是该标识符的声明:
- 对于一个对象,导致为该对象保留存储;
(......)
那么块内变量的声明也是一个定义吗?例如:
void Bla(void) {
int a; // Is this declaration also a definition?
}
Run Code Online (Sandbox Code Playgroud)
我在stackoverflow上找到了以下两个答案,其中声明块内变量的声明也是定义:
但是,"Michael Burr"提供的答案是指6.2.2/2"标识符的链接",以便解释块范围内的变量声明也是定义.对我来说,他的参考不回答我的问题.第二个链接中的答案未提供对C标准的任何引用.C标准中是否还有其他段落可用作确认答案的参考?
请提供C标准的参考.
declaration ×10
c++ ×4
c ×2
constructor ×2
javascript ×2
c++11 ×1
c11 ×1
definition ×1
fortran ×1
function ×1
object ×1
reference ×1
rust ×1
variables ×1
visibility ×1