小编bil*_*llz的帖子

实际类的数组

  Ship *ship;

  if (newShipType == 0)
  {
    ship = new SmallShip(gridPosition.x, gridPosition.y,
                         grid->raw);
  }
  else if (newShipType == 1)
  {
    ship = new MediumShip(gridPosition.x, gridPosition.y,
                          grid->raw);
  }
  else // 2
  {
    ship = new BigShip(gridPosition.x, gridPosition.y,
                       grid->raw);
  }
Run Code Online (Sandbox Code Playgroud)

我有这个代码,我希望通过以下方式简化:

Ship *ship = new getShipByType[newShipType](gridPosition.x, gridPosition.y, grid->raw);
Run Code Online (Sandbox Code Playgroud)

有可能吗?

Ship getShipByType[3] = {SmallShip, MediumShip, BigShip};
Run Code Online (Sandbox Code Playgroud)

这给了:

error: expected primary-expression before ‘,’ token
error: expected primary-expression before ‘,’ token
error: expected primary-expression before ‘}’ token
Run Code Online (Sandbox Code Playgroud)

我真的不希望它编译,只是寻找一种更简单的方法,这只是一个非常长的尝试.

c++ oop design-patterns

3
推荐指数
1
解决办法
135
查看次数

C++临时和构造函数

为什么有些编译器抱怨从构造函数中获取临时地址而有些不?

例如:

WriteLine(&String8("Exception"));
Run Code Online (Sandbox Code Playgroud)

我假设抱怨编译器没有将该地址存储在堆栈中,如果我忽略该警告,它将会爆炸.有没有办法让它在一条线上工作?是返回地址的类的静态方法的最佳/唯一方法吗?嗯我不认为String8类上返回指针的静态方法会起作用,因为它会将String8类保存到寄存器然后在WriteLine()中使用它

c++

2
推荐指数
1
解决办法
207
查看次数

C++:使用2个函数重载,一个在头文件中,一个不是

假设我在一个头文件中声明了(在c ++,VS 2010中)一个名为" void f(int x) " 的函数,然后在相应的cpp文件上实现它.在尝试仅在该源文件中添加重载函数(void f(int x,int y))时(标题中没有声明),我收到错误" 函数不带2个参数 ".
(此函数写在调用函数之上).

头文件没有任何实现的代码.

我是否违反了某些c ++规则,或者仅仅是因为使用了Visual?我必须在头文件中声明所有重载函数(或根本没有)吗?

编辑:源文件:

int findNodeRec(int data, NodeTree *root) 
{ 
    return 1;
}
int Tree::findNodeRec(int data) 
{ 
    return findNodeRec(data, m_root); 
} 
Run Code Online (Sandbox Code Playgroud)

c++ overloading header-files

2
推荐指数
1
解决办法
1697
查看次数

为什么我不能使用fstream矢量?

当我尝试使用a时vector <fstream>,它会输出编译错误.为什么C++禁止fstream的向量?

c++ fstream vector c++11

2
推荐指数
1
解决办法
324
查看次数

C ++ find_if找不到谓词

我收到错误消息:

no matching function for call to ‘findByPosition::findByPosition(std::vector<int>::size_type&, std::vector<int>::size_type&)’
Run Code Online (Sandbox Code Playgroud)

当我投ikint我得到:

no matching function for call to ‘findByPosition::findByPosition(int, int)’
Run Code Online (Sandbox Code Playgroud)

我不知道我的谓词有什么问题。我已经()根据需要重载了运算符:

struct findByPosition
{
    const Node needle;
    findByPosition(const Node& sought) : needle(sought) {}
    bool operator()(int i,int j) const
    {
        return ((needle.i == i) && (needle.j == j));

    }
};

SparseMatrix& SparseMatrix::operator*=(const SparseMatrix &other)
{
    SparseMatrix SparseMatrixResult(_numRow, other._numCol);
    vector<Node>::iterator rowMulti, colMulti;

    if(_numCol != other._numRow)
    {
        // error multiplying
    }

    for(std::vector<int>::size_type i = 0; i != …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm vector

2
推荐指数
1
解决办法
766
查看次数

无法将元素添加到链接列表的末尾

我试图在链接列表的后面添加一个元素但它似乎没有添加,有人可以告诉我我的代码有什么问题谢谢.我的链接列表不为空

void LinkedList::Addelementfromback(VoidPtr horoscope)
{
   NodePtr temp = head;  

   while (temp != NULL)
   {
    temp=temp->next;
   }

    NodePtr element=new Node;

    element->data=horoscope;

    element->next=NULL;

    temp=element;
}
Run Code Online (Sandbox Code Playgroud)

c++ debugging linked-list

2
推荐指数
1
解决办法
1298
查看次数

按一个在c ++中带参数的函数排序?

我正在尝试编写一个函数来通过各种不同的属性对自定义类对象的向量进行排序.

c ++排序参考,在这里找到:

http://www.cplusplus.com/reference/algorithm/sort/

说你可以像这样排序:

std::sort (myvector.begin(), myvector.end(), myfunction);
Run Code Online (Sandbox Code Playgroud)

除了我的向量中的两个对象之外,我希望能够做的是将参数传递给myfunction:

std::sort (myvector.begin(), myvector.end(), myfunction(mode=7));
Run Code Online (Sandbox Code Playgroud)

你知道这样做的方法吗?

我对c ++比较陌生,来自python,这很容易.

c++ sorting

2
推荐指数
1
解决办法
89
查看次数

模板转换操作符C++

我正在尝试制作一个小包装类,如

template <typename T>
class EdgeTriggeredState

{

public:
    void Tick()
    {
        oldData = newData;
    }

    EdgeTriggeredState& operator =(const T& v)
    {
        newData = v;
        return *this;
    }

//  T& operator = (void)
//  {
//      return oldData;
//  }

//  T& operator T()
//  {
//      return oldData;
//  }

private:
    T oldData;
    T newData;
};
Run Code Online (Sandbox Code Playgroud)

基本上我希望能够直接为类包装的值赋予类型为T的变量.我已经尝试实现了一个赋值(到类型T)操作符和一个强制转换操作符来输入T.我在C++上有点生疏,因为我一直在C中工作.有没有办法实现这个而不创建一个命名的getter方法?

当我取消注释第一次实现尝试时,我得到错误

"../EdgeTriggeredState.h:19:21:错误:'T&EdgeTriggeredState :: operator =()'必须只接受一个参数"

当我取消注释第二个实现(并注释掉第一个实现)时,我得到错误:

"../EdgeTriggeredState.h:24:16:错误:为'运营商T'指定的返回类型"

c++ casting operator-overloading

2
推荐指数
1
解决办法
2955
查看次数

将字符串转换为const char*issue

string str1 = "hello";
    const char* string1 = str1; 
Run Code Online (Sandbox Code Playgroud)

我收到一个错误..

在初始化时无法将'std :: string {aka std :: basic_string}'转换为'const char*'

我如何将字符串转换为const char*

谢谢你的帮助

c++ string

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

在一组c ++中访问字符串的第一个字母

我有一组字符串,我需要访问每个字符串,并将字符串的第一个字母与一个char进行比较,如果它们相同则停止。我该如何处理?我尝试过

char* p;

for (std::set<string>::iterator iter=myset.begin(); iter!=myset.end();iter++)
    {p = *iter;
    if (p==characterForComparison) return 0;
    }
Run Code Online (Sandbox Code Playgroud)

但这并没有通过编译器,它说

error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char *'
Run Code Online (Sandbox Code Playgroud)

我该如何解决?我需要的非常简单,因此我需要一个尽可能简单的解决方案,只需遍历所有字符串,比较第一个字母,如果相同,则返回0;否则,返回0。

c++ string iterator set

2
推荐指数
1
解决办法
1903
查看次数