小编Dan*_*dox的帖子

没有默认构造函数的对象数组初始化

#include <iostream>
class Car
{
private:
  Car(){};
  int _no;
public:
  Car(int no)
  {
    _no=no;
  }
  void printNo()
  {
    std::cout<<_no<<std::endl;
  }
};
void printCarNumbers(Car *cars, int length)
{
    for(int i = 0; i<length;i++)
         std::cout<<cars[i].printNo();
}

int main()
{
  int userInput = 10;
  Car *mycars = new Car[userInput];
  for(int i =0;i < userInput;i++)
         mycars[i]=new Car[i+1];
  printCarNumbers(mycars,userInput);
  return 0;
}    
Run Code Online (Sandbox Code Playgroud)

我想创建一个汽车阵列,但我收到以下错误:

cartest.cpp: In function ‘int main()’:
cartest.cpp:5: error: ‘Car::Car()’ is private
cartest.cpp:21: error: within this context
Run Code Online (Sandbox Code Playgroud)

有没有办法在不使Car()构造函数公开的情况下进行初始化?

c++ arrays constructor

60
推荐指数
6
解决办法
10万
查看次数

Hashtable与双链表?

算法简介(CLRS)指出,使用双向链表的哈希表能够比单链表更快地删除项.谁能告诉我在Hashtable实现中使用双链表而不是单链表进行删除有什么好处?

algorithm hashtable

9
推荐指数
1
解决办法
5188
查看次数

如何自动生成c ++标头

鉴于foo.hpp

class Foo
{
public:
   void MethodA()
   {
        //BODY IMPLEMENTATION
   }
   int MerhodB()
   {
       return 2+3;
   }
}
Run Code Online (Sandbox Code Playgroud)

我需要一个工具来生成两个文件foo.cpp和foo.hpp.如

foo.hpp

class Foo
{
public:
   void MethodA();
   int MerhodB();
}
Run Code Online (Sandbox Code Playgroud)

Foo.cpp中

void Foo::MethodA()
{
     //BODY IMPLEMENTATION
}
int Foo::MerhodB()
{
    return 2+3;
}
Run Code Online (Sandbox Code Playgroud)

我试过Lazy c ++,但它不适合我.还有其他建议吗?

编辑:我不使用Windows所以请避免使用Visual Studio工具.谢谢

c++ header

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

在C中检测文件系统

有没有办法判断文件是在本地磁盘还是在C中的NFS?代码应该可以在各种Linux发行版中移植,并且不应该依赖于系统调用(例如stat -f).

c linux

4
推荐指数
1
解决办法
1533
查看次数

标签 统计

c++ ×2

algorithm ×1

arrays ×1

c ×1

constructor ×1

hashtable ×1

header ×1

linux ×1