标签: declaration

C++嵌套类转发声明错误

我试图在类A中声明并使用类B并在A之外定义B.
我知道这是可能的,因为Bjarne Stroustrup
在他的书"The C++ programming language"
(例如,第293页)中使用了它String和Srep类).

所以这是导致问题的最小代码

class A{
struct B; // forward declaration
B* c;
A() { c->i; }
};

struct A::B { 
/* 
 * we define struct B like this becuase it
 * was first declared in the namespace A
 */
int i;
};

int main() {
}
Run Code Online (Sandbox Code Playgroud)

此代码在g ++中提供以下编译错误:

tst.cpp: In constructor ‘A::A()’:
tst.cpp:5: error: invalid use of undefined type ‘struct A::B’
tst.cpp:3: error: forward declaration of ‘struct A::B’
Run Code Online (Sandbox Code Playgroud)

我试着看看C++ Faq,我得到的密集在这里和这里,但 …

c++ nested class declaration forward

7
推荐指数
3
解决办法
2万
查看次数

在函数声明中包含参数名称与不包含参数名称之间的区别是什么?

我记得以前在阅读函数声明中包含参数名称而不包括参数名称之间的重要性(或缺乏重要性).但我不记得我读到的是什么,或者我在哪里阅读它.

例如,

void do_something(int *); // No parameter name included, only type.
Run Code Online (Sandbox Code Playgroud)

VS ...

void do_something(int * i); // type AND parameter name included.
Run Code Online (Sandbox Code Playgroud)

那么这两个声明之间的区别是什么?感谢阅读,也许回答这个可能微不足道的问题.

- 更新 -

好吧,所以我读过的东西是来自我的一位老教授的一套风格指南,警告不要在函数定义中包含参数名称而不要在函数中使用参数.

void do_something(int * i) { //code that doesn't use i;} //BAD
void do_something(int *) { //code that doesn't use i;} //OK
Run Code Online (Sandbox Code Playgroud)

c++ declaration function

7
推荐指数
1
解决办法
1443
查看次数

C++字符串变量声明

我在声明字符串变量时遇到了一些麻烦.代码和错误在这里:http://pastebin.com/TEQCxpZd任何关于我做错了什么的想法?另外,请保持平台独立.谢谢!

#include <stdio.h>
#include <string>
using namespace std;

int main()
{
    string input; //Declare variable holding a string

    input = scanf; //Get input and assign it to variable
    printf(input); //Print text
    return 0;
}


Getting this from GCC:

main.cpp: In function ‘int main()’:
main.cpp:53:10: error: invalid conversion from ‘int (*)(const char*, ...)’ to ‘char’
main.cpp:53:10: error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, …
Run Code Online (Sandbox Code Playgroud)

c++ string variables declaration

7
推荐指数
1
解决办法
5万
查看次数

为什么Java中的字符串变量声明大写?

在Java中,当一个声明一个字符串变量时,单词"String"被大写,但它不在我遇到的任何其他类型中(例如"int"或"double").为什么是这样?这只是设计师的一些奇怪的武断决定吗?

java string variables types declaration

7
推荐指数
1
解决办法
3527
查看次数

混淆'sizeof'运算符的结果

我最近尝试过这段代码并且有点困惑.请参阅以下声明:

 static   st;
 auto     au;
 register reg;
 volatile vl;
 const    cn;
Run Code Online (Sandbox Code Playgroud)

它们都分配4个字节的内存(在32位GCC上).但是当我尝试打印(使用printf功能)他们的尺寸时,他们没有工作并且出错.

  sizeof(const)      // worked and printed 4
  sizeof(volatile)   // worked and printed 4

  sizeof(auto)       // error: expected expression before ‘auto’
  sizeof(static)     // error: expected expression before ‘static’
  sizeof(register)   // error: expected expression before ‘register’
Run Code Online (Sandbox Code Playgroud)

我怀疑是auto, static, register 关键字还分配4字节的内存(在32位拱门上).

但是,为什么这些是给错误不像const和volatile?

c gcc declaration sizeof

7
推荐指数
2
解决办法
763
查看次数

哪一个错了?Msdn或Headerfile

我目前正在移植IAudioVolumeDuckNotification到C#.但是该OnVolumeUnduckNotification方法的声明存在一个问题.Msdn告诉我这个: 在此输入图像描述

我的"audiopolicy.h"头文件告诉我这个: 在此输入图像描述

如您所见,在msdn-version中有第二个参数.我很困惑.哪一个是正确的?我应该使用哪一个,哪里可以报告错误?

根据要求链接到msdn页面:http://msdn.microsoft.com/en-us/library/windows/desktop/dd371012( v = vs.85).aspx

c# c++ com declaration

7
推荐指数
1
解决办法
134
查看次数

为什么变量声明总是会覆盖函数声明?

无论我是否在变量后定义函数

var a = 1;
function a() {};
typeof a // number
Run Code Online (Sandbox Code Playgroud)

或者如果我在变量之前定义函数

function a() {};
var a = 1;
typeof a // number
Run Code Online (Sandbox Code Playgroud)

最后的typeof结果总是如此number

我execution context在http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/找到了一些解释

Before executing the function code, create the execution context.
......
Scan the context for variable declarations:
If the variable name already exists in the variable object, do nothing and continue scanning.
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.

那我怎么解释呢?

javascript variables declaration function

7
推荐指数
1
解决办法
604
查看次数

do-while声明正文中的声明范围

在你为什么不能声明while循环里面做的一个变量?OP询问为什么do-while循环的while-condition中的声明不在do语句的范围内.这将是非常不自然的,因为C/C++通常遵循"顶级声明"模式.但是相反的是 - 为什么不将do语句中的任何声明的范围扩展到while条件.这样就可以了

int i;
do {
  i = get_data();
  // whatever you want to do with i;
} while (i != 0);
Run Code Online (Sandbox Code Playgroud)

被缩短为

do {
  int i = get_data();
  // whatever you want to do with i;
} while (i != 0);
Run Code Online (Sandbox Code Playgroud)

它为限制控制变量的范围提供了一个整洁的语法.(注意:我知道这不是有效的语法 - 这就是问题的关键,为什么不扩展语言以允许这种语法.)

正如我在下面的评论中指出的那样,这个扩展不会破坏现有代码,并且非常符合引入for-init(和while-init)范围的精神.

c++ declaration language-lawyer do-while

7
推荐指数
1
解决办法
1042
查看次数

C++ :: #include:多个源文件中的模板类头文件?

我正在使用这个类 - 除了一个 - 模板成员函数之外,它将在具有多个源文件的项目中使用,这些源文件在编译时链接.模板类型未知,可以采用任何类型.在这种情况下,我有两个使用该类的源文件,因此具有类声明和定义的头文件在两个源文件中都是#include:ed.然后,我在类的非模板成员函数声明中得到错误"multiple definition".我认为这是因为它在链接过程中被定义了两次,因为两个源文件都有非模板成员函数的定义.想象一下下面的无意义场景:

注意:假设所有文件都包含防护,iostream在需要的地方为#include:ed.

foo.hpp

class foo
{
public:
    template <typename X>
    void f(X);

    void ff ();
};

#include "foo.tpp"
Run Code Online (Sandbox Code Playgroud)

foo.tpp

template <typename X>
void foo::f(X val)
{
    cout << val;
}

void foo::ff() // multiple definitions
{
    cout << sizeof(*this);
}
Run Code Online (Sandbox Code Playgroud)

main2.cpp

#include "foo.hpp"
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "foo.hpp"

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

将inline关键字添加到函数定义似乎解决了这个错误,虽然我不想使用它,因为我有其他非模板成员函数遇到相同的问题,这些函数更大并且在多个部分引用码.是否有任何解决方法或有效的方法来做我正在尝试做的事情?提前致谢!

c++ templates declaration definition

7
推荐指数
1
解决办法
708
查看次数

语法-变量声明中的方括号含义是什么

采取以下代码行

const [component] = router.getMatchedComponents({ ...to })
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议组件周围的方括号在这里意味着什么吗?我试图用谷歌搜索这个,但是努力寻找答案

javascript arrays variables declaration object

7
推荐指数
1
解决办法
2471
查看次数