我试图在C++中创建一个异常类,但它不起作用。我已经将代码减少到最低限度,但仍然找不到错误。下面是我的头文件:
#ifndef LISTEXCEPTION_H
#define LISTEXCEPTION_H
// C++ standard libraries
#include <exception>
/* CLASS DEFINITION */
class ListException: public exception {
};
#endif //LISTEXCEPTION_H
下面是我得到的错误:
error: expected class-name before ‘{’
#include <iostream>
using namespace std;
int main() {
int i = 0;
if(true) {
int i = 5;
cout << i << '\n';
}
return 0;
}
试着在Ideone上运行上面的代码,看看它是否合法。结果使我感到困惑:
我们有一个编译错误,
或此代码将5打印为预期的。
或者它什么都不打印,
从我的链接中可以看到,每次在Ideone上编译这些代码时,这些代码的行为都会发
我在我的pro*C程序中有这些代码。函数initAverage(int )是用C语言定义的,我正在尝试在.pcc ( C++)文件中调用该函数。
我收到一个错误
Error: initAverage(int i);was declared before with a different language
extern "C"
{
int initAverage(int i);
}
当出现不明确的默认参数时,C++编译器会做什么?例如,假设有一个函数,例如:
void function(int a = 0, float b = 3.1);
void function(int a, float b =1.1, int c = 0);
以上内容是否被认为是模棱两可的?如果不是,那么编译器在调用function1(10)之类的东西时会做什么(函数是如何精确匹配的)?
谢谢!
在C语言中,如果我们这样写:
for(int i = 0; i < 7; i++)
{
// for loop Body
}
变量i的作用域位于for循环体内。没问题。
但是,如果我写这个:
for(int i = 0; i < 7; i++)
{
long int i = 1; // Redeclaration of i
}
在这里,变量i在循环体中再次声明,但是它在C中成功编译和运行。
但是,在C++中,编译器给出了一个“长int I‘的重声明”错误。
那么,为什么C编译器不提供一个重声明错误呢?是编译器的错误吗?
我试图在我的ubuntu11.10上编写VW34(牛津主动视觉实验室库)。我已经准备好了它所需要的唇语。我已经通过apt-get获得了mesa-libgl1 dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev libGlu3-dev。我把所有东西都放在同一个文件夹里了。我跟踪./引导。/配置..。流。配置脚本是生成的购买引导,所以我认为不应该有一个错误。ubuntu支持VW34吗?
wuyi@wuyi-notebook:~/下载/monoslam/VW34$ ./configure
checking for a BSD-compatible i
在下文中,我不知道我是否混淆了C#中的枚举和C++,但我认为您只能使用Forms::shape访问枚举中的枚举数,这实际上会给出一个错误。
int main()
{
enum Forms {shape, sphere, cylinder, polygon};
Forms form1 = Forms::shape; // error
Forms form2 = shape; // ok
}
为什么允许在没有作用域运算符的情况下在枚举外部访问shape?如何防止这种行为?
考虑一下C程序的这段代码:
for(int i = 0; i < 5; i++)
{
int i = 10; // <- Note the local variable
printf("%d", i);
}
它编译时没有任何错误,并在执行时提供以下输出:
1010101010
但是,如果我用C++编写类似的循环:
for(int i = 0; i < 5; i++)
{
int i = 10;
std::cout << i;
}
此错误导致编译失败:
prog.cc:7:13: error: r
我想知道如何访问数组内的类中的属性,如下面的示例:
import java.util.*;
public class DogTest{
public class Dog {
int Quantity;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Dog dogs[] = new Dog[15];
for ( int i = 1; i <=15; i++){
我正在学习C++,并且在将这个设计模式从C#中带过来时遇到了麻烦。我不知道是这个模式在C++中不起作用,还是我的语法不正确。
在本例中,我想创建一个解码位集的类。
在带有泛型的C#中,它看起来像这样:
abstract class Base<T>
{
public abstract T GetValue(BitArray bits);
}
class Derived : Base<MyType>
{
public override MyType GetValue(BitArray bits)
{
// Do some magic to decode
我在javascript中已经有很长一段时间了,但我不能理解以下情况:
var a = "test1";
var c = function()
{
console.log("2/ "+a)
if(true)
{
console.log("3/ "+a);
}
else
{
var a = "test2";
console.log("3/ "+a);
}
}
console.log("1/
# include <iostream>
enum any : int; // unscoped opaque declaration :int required by the standard
enum : int {a} t; // unscoped anonymous declaration of t (:int not required)
enum any : int {b} u; // redlecaration of type "any" with one enumerator
enum class foo : char; // s
function f() { return s; } // works fine though no `s` is defined yet !
var s=1;
f(); // 1
delete s;
var s=2;
f(); // 2
(function() {
var s=3;
f(); // 2 and not 3 which means lexical scoping is at play (?)
})();
首先,您可以关闭一个尚未定义的变量(s)。如果使用词法(静态)作用域,这是如何实现的?
第二,删除原s后,f()可以找到新的s。这是否意味着闭包绑定到变量名,而不是引
以下程序是否符合c++标准?
namespace X { class A; }
namespace Y { using X::A; class A {}; }
int main() {}
通过不同的编译器,我得到了不同的结果:
gcc没有错误地编译它。
visual c++给出错误C2888:'X::A':符号不能在命名空间'Y‘内定义
在c++标准中,我没有发现我的程序违反了任何规则。
如果程序格式良好,为什么visual studio会出现错误?
如果程序格式不完善,那么c++标准中的哪些规则被违反了,为什么gcc没有给出一个错误?
我不是想让我
考虑下面的程序。它是否符合c++标准(需要参考标准的相关部分):
namespace X { extern int i; }
namespace N { using X::i; }
int N::i = 1;
int main() {}
我对不同的编译器得到了不同的结果。我正在尝试弄清楚我应该为哪个编译器提交错误报告:
Clang:给出以下编译器错误:命名空间‘N’中没有名为'i‘的成员GCC和Visual C++编译它时没有错误。
为了进行比较,下面给出了所有三个编译器的编译器错误:
namespace X { void f(); }
namespace N { using
在头文件中声明静态函数时,我遇到了这个问题。假设我有这个文件"test.cpp":
#include <cstdlib>
#include "funcs.hpp"
...do some operations...
void *ptr = my_malloc(size);
...do some operations...
"funcs.hpp“文件包含:
#include <ctime>
#include "../my_allocator.hpp" // my_malloc is defined here as
如果在C++中定义了一个新变量,则可以在初始化表达式中使用该变量的名称,例如:
int x = sizeof(x);
那么函数参数的默认值呢?它是否允许在那里引用其名称的论点?例如:
void f(int y = sizeof(y)) {}
该函数在Clang中被接受,但在GCC中被拒绝,错误如下:
'y' was not declared in this scope
演示:
哪个编译器就在这里?