标签: declaration

是否可以在不实现的情况下声明一个类?(C++)

我知道这些问题似乎含糊不清,但我想不出任何其他方式来表达它,但是,是否有可能做到这样的事情:

#include<iostream>

class wsx;

class wsx
{
public:
wsx();
}

wsx::wsx()
{
std::cout<<"WSX";
}
Run Code Online (Sandbox Code Playgroud)

?

c++ class declaration definition forward-declaration

8
推荐指数
1
解决办法
4179
查看次数

"Object [] x"和"Object x []"之间有什么区别吗?

我正在用Java更新遗留代码库,我找到了这样的一行:

Object arg[] = { new Integer(20), new Integer(22) };
Run Code Online (Sandbox Code Playgroud)

那条线引起了我的注意,因为我习惯了这种代码:

Object[] arg = { new Integer(20), new Integer(22) };
Run Code Online (Sandbox Code Playgroud)

这里的数组内容并不重要.我很好奇变量名旁边的括号与类名旁边的括号.我在Eclipse(使用Java 5)中尝试过,这两行对编译器都有效.

这些声明之间有什么区别吗?

java arrays declaration

8
推荐指数
2
解决办法
1536
查看次数

为什么这个单独的定义会导致错误?

挑战:

我有这个代码无法编译.你能弄明白什么是错的吗?它给我带来了一次头痛.

// header
namespace values {
  extern std::string address;
  extern int port;
}

// .cpp file
std::string  ::values::address = "192.0.0.1";
int          ::values::port    = 12;
Run Code Online (Sandbox Code Playgroud)

第一眼看上去是正确的.有多少,哪些是错误!?

c++ header declaration extern

8
推荐指数
1
解决办法
457
查看次数

如何在C的头文件中正确编写extern数组(和双数组)的声明?

假设我想在我的程序中共享全局数据数组,例如:

int lookup_indexes[] = { -1, 1, 1, -1, 2, 1, 1, -2, 2, 2, -1, 1, 1, 2 };
Run Code Online (Sandbox Code Playgroud)

externC头文件中此数组的正确声明是什么?

那么这样的数组怎么样:

int double_indexes[][5] = { { -1, 1, 1, -1, 1 }, { 2, -2, 2, 1, -1 } };
Run Code Online (Sandbox Code Playgroud)

在我的头文件中我尝试了这个:

extern int lookup_indexes[];
extern int double_indexes[][5];
Run Code Online (Sandbox Code Playgroud)

但这会导致编译器错误:

water.h:5: error: array type has incomplete element type
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚.

谢谢,Boda Cydo.

c arrays header declaration extern

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

模糊的声明

以下两个声明之间有什么区别:

 1. int foo(int); 
 2. int foo(int());
Run Code Online (Sandbox Code Playgroud)

我不确定这两个声明是否相同.有什么(2)不同(1)?

c c++ declaration function

8
推荐指数
1
解决办法
351
查看次数

名称空间,类和自由函数 - 何时需要完全限定名称

在下面的示例中,为什么我必须完全限定cpp中自由函数的名称以避免链接器错误,为什么它不适用于类函数?你能解释一下这个区别吗?

ctest.h:

namespace Test
{
    int FreeFunction();

    class CTest
    {
        public:
            CTest();
            ~CTest();
    };
}
Run Code Online (Sandbox Code Playgroud)

ctest.cpp:

#include "ctest.h"

using namespace Test;

// int FreeFunction()     -> undefined reference error
int Test::FreeFunction()  -> works just fine
{
    return 0;
}

CTest::CTest()                -> no need to fully qualify name, i.e. Test::CTest
{}

CTest::~CTest()
{}
Run Code Online (Sandbox Code Playgroud)

感谢您的时间和帮助.

c++ namespaces declaration definition fully-qualified-naming

8
推荐指数
1
解决办法
3563
查看次数

析构函数是否应该用指针声明OR?在C++中

在C++ 0x -n3290草案中:他们在章节中添加了:Destructors:12.4/2nd point last line

          **A destructor shall not be declared with a ref-qualifier.**
Run Code Online (Sandbox Code Playgroud)

在c ++ 03 Draft ....他们没有在析构函数中提到这一点?

我的问题是,是否

   *~S() ;   //this declaration is allowed or not according to the Standard's
   //**~S(); ***~S() ; etc...........
Run Code Online (Sandbox Code Playgroud)

允许这种类型的声明?他没有在草案中描述过这个......宣言?

在GCC 4.6.0中,Sun/Oracle C++ 12.0,--->这个声明允许使用Comeau C/C++ - >不允许

c++ destructor declaration c++11

8
推荐指数
2
解决办法
237
查看次数

C++错误:找不到成员声明

我是一个c ++新手.今天,我有一个问题:在头文件中,我定义了一个类:

template<class T> class Ptr_to_const {
private:
    Array_Data<T>* ap;
    unsigned sub;

public:
        ...

    Ptr_to_const<T> & operator=(const Ptr_to_const<T> & p);

};
Run Code Online (Sandbox Code Playgroud)

在源文件中,我编程为:

template<class T> Ptr_to_const<T>& Ptr_to_const<T>::operator=(
        const Ptr_to_const<T> & p) {
         ...
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

编译时,编译器总是说:" 未找到成员声明 ". 为什么?

我使用eclipse CDT + Cygwin GCC

非常感谢你!

c++ declaration member

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

前向声明和循环依赖

我有两个课程,实体和水平.两者都需要访问彼此的方法.因此,使用#include会产生循环依赖的问题.因此,为了避免这种情况,我尝试在Entity.h中转发声明Level:

class Level { };
Run Code Online (Sandbox Code Playgroud)

但是,由于Entity需要访问Level中的方法,因此它无法访问此类方法,因为它不知道它们存在.有没有办法解决这个问题,而无需重新声明实体中的大部分级别?

c++ dependencies declaration circular-dependency forward

8
推荐指数
2
解决办法
8886
查看次数

是否为HashSet或其他实现使用Java变量类型Collection?

我经常看到类的声明List<String> list = new ArrayList<>();或Set<String> set = new HashSet<>();类的声明。对我来说,使用变量类型的接口来提供实现的灵活性是非常有意义的。上面的示例仍然定义Collection了必须使用的类型,分别允许的操作以及在某些情况下的行为(由于docs)。

现在考虑以下情况:使用类中的字段实际上仅需要Collection(或什至Iterable)接口的功能,Collection而实际上的类型无关紧要,或者我不想对其进行过多说明。因此,我选择例如HashSet作为实现,并将字段声明为Collection<String> collection = new HashSet<>();。

那么,Set在这种情况下,该字段实际上是否应该是类型?这样的声明是不好的做法吗,如果是这样,为什么?还是最好的做法是指定尽可能少的实际类型(并仍然提供所有必需的方法)。之所以这样问,是因为我几乎从未见过这样的声明,而最近在只需要指定Collection接口功能的情况下,我会得到更多的信息。

例:

// Only need Collection features, but decided to use a LinkedList
private final Collection<Listener> registeredListeners = new LinkedList<>();

public void init() {
    ExampleListener listener = new ExampleListener();
    registerListenerSomewhere(listener);
    registeredListeners.add(listener);
    listener = new ExampleListener();
    registerListenerSomewhere(listener);
    registeredListeners.add(listener);
}

public void reset() {
    for (Listener listener : …
Run Code Online (Sandbox Code Playgroud)

java collections types interface declaration

8
推荐指数
1
解决办法
151
查看次数