小编sjd*_*ing的帖子

如何返回不可用的const引用

说我有以下功能:

const std::string& Cat::getKittenName() const
{
  Kitten* kitty = getKitty();
  return kitty->getName();
}
Run Code Online (Sandbox Code Playgroud)

哪里Kitten::getName返回一个const std::string&如何最好地处理的情况kittynullptr哪个?我可以返回,std::string("")但后来我返回一个临时和实际保证未定义行为的引用.我可以更改getKittenName函数返回一个std::string来解决这个问题,但后来我为所有kitty可用的情况引入了一个冗余副本.现在我觉得最好的选择是:

const std::string& Cat::getKittenName() const
{
  Kitten* kitty = getKitty();
  if (kitty)
  {
    return kitty->getName();
  }
  static std::string empty("");
  return empty;
}
Run Code Online (Sandbox Code Playgroud)

唯一的问题可能是"魔法静力学"不可用.这个解决方案有什么问题,或者有更好的方法吗?

c++ c++11

17
推荐指数
2
解决办法
3881
查看次数

QML TableView具有动态列数

我一直在尝试使用QML TableView来显示QAbstractTableModel.等式中缺少的部分似乎是不可能有一个可变数量的列TableView,尽管覆盖QAbstractItemModel::roleNames哪个应该告诉Qt我的列的数量和名称.我尝试仅使用QML进行测试:

import QtQuick 2.0
import QtQuick.Controls 1.1

Rectangle {
    anchors.fill: parent
    property real showImage: 1.0
    width: 500
    TableView {
        id: myTable
        model: myModel
        //        TableViewColumn {
        //            role: "title"; title: "Name"; width: 200
        //        }
    }

    ListModel {
        id: myModel
        ListElement {
            title: "item one"
        }
        ListElement {
            title: "item two"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在运行时,尽管TableView包含ListElement在其中定义了角色的s的模式,但这并未显示任何内容.

但是,如果上面的代码被取消注释并且TableViewColumn定义了a ,则该列将按预期显示该角色的数据,但该表仍将不显示任何其他角色.显然,这只适用于静态定义的列数,而不适用于直到运行时才知道列数的情况.

给出的示例与我的实际示例基本相同,只是我的模型是用C++定义的.

似乎这可能已经在这里被问过,但它没有得到任何回应.

编辑:我曾尝试调用javascript函数:

function …
Run Code Online (Sandbox Code Playgroud)

c++ qt qml qtquick2

10
推荐指数
1
解决办法
7503
查看次数

xcode 7如何禁止警告"覆盖成员函数但未标记为'覆盖'"

我今天将xcode更新为7.更新后,我正在处理的项目有"覆盖成员函数但未标记为'覆盖'"的警告.由于我们的项目将"treade warning as error"设置为true.我有很多错误.

我仔细检查了"其他链接器标志"并且非常确定不包括标志"-Wsuggest-override".

由于这是一个大项目,我更喜欢禁止警告,而是在任何地方添加关键字"覆盖".

有关禁用警告的任何建议吗?

谢谢

c++ xcode clang c++11

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

Visual Studio 2015表示'演员是多余的'.为什么?

我的图像宽度为888像素,高度为592像素,宽高比为3:2.

由于BitmapDecoder.PixelWidth和BitmapDecoder.PixelHeight都是uint(无符号整数),decoder下面是BitmapDecoder对象,因此下面会产生1的错误值,因为整数计算/截断.

double aspectRatio = decoder.PixelWidth / decoder.PixelHeight;

下面给出了1.5的预期正确值,但Visual Studio说"Cast是多余的",但为什么呢?

double aspectRatio = (double)decoder.PixelWidth / (double)decoder.PixelHeight;

c# visual-studio-2015 uwp

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

如何使QML容器中的最后一项填充剩余空间?

我希望动态调整大小的窗口具有列布局,以便列中的最后一项填充剩余的剩余空间.我可以通过动态计算javascript中最后一项的高度来做到这一点.我还可以将最后一项移出列并将顶部绑定到列的底部和容器的底部,但是我还必须从其内容中计算列的新大小.

import QtQuick 2.0
import QtQuick.Controls 1.1

Rectangle {
    id: rect
    anchors.fill: parent
    Column {
        id: myColumn
        anchors.fill: parent
        Rectangle {
            id: container
            signal clicked
            width: label.width + 20; height: label.height + 6
            anchors { right: parent.right }
            border.width: 1
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked:  {
                    if (myColumn.state == 'hidden') { myColumn.state = '' }
                    else { myColumn.state = 'hidden'; }
                }
            }
            Text {
                id: label
                text: { if (myColumn.state == '') {"Hide"} else {"Show"} }
                anchors.centerIn: …
Run Code Online (Sandbox Code Playgroud)

c++ qt qml

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

使用无捕获的lambda表达式作为条件运算符的第二个和第三个操作数时出现MSVC错误

下面的代码很高兴被GCC和Clang接受,-std=c++14但导致Visual Studio 2013出现编译错误.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    auto increasing = [](int lhs, int rhs){return lhs < rhs;};
    auto decreasing = [](int lhs, int rhs){return lhs > rhs;};
    std::vector<int> v(0, 10);
    bool increase = true;
    std::sort(v.begin(), v.end(), increase ? increasing : decreasing);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误是:

main.cpp(11): error C2446: ':': no conversion from 'main::<lambda_0228ee097b83254cfd8aa5f4015a245b>' to 'main::<lambda_cb3b816d067baa9d4462132ee332363c>' main.cpp(11): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot …

c++ visual-c++ language-lawyer c++11 visual-studio-2013

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

如何使用RcppEigen得到矩阵的行列式

我是Rcpp的新手.我试图使用R包RcppEigen来获得矩阵的行列式.以下代码保存在文件中,我使用sourceCpp来使用它.使用sourceCpp时没有编译错误.getDeterminant(A)在R中使用时,A是一个矩阵.它总是抱怨以下错误.

"Error: could not find function "getDeterminant""
Run Code Online (Sandbox Code Playgroud)

但是,getEigenValues效果很好.

如果有人乐意帮助我,我会非常感激.非常感谢!

#include <RcppEigen.h>

// [[Rcpp::depends(RcppEigen)]]

 using Eigen::Map;                 // 'maps' rather than copies 
 using Eigen::MatrixXd;                  // variable size matrix, double  precision
 using Eigen::VectorXd;                  // variable size vector, double precision
 using Eigen::SelfAdjointEigenSolver;    // one of the eigenvalue solvers
 using Eigen::MatrixXi;
 using Eigen::MatrixBase;
 // [[Rcpp::export]]
 VectorXd getEigenValues(Map<MatrixXd> M) {
     SelfAdjointEigenSolver<MatrixXd> es(M);
     return es.eigenvalues();
 }

// [[Rcpp:export]]
double getDeterminant(Map<MatrixXd> AA){
     return  AA.determinant();
}
Run Code Online (Sandbox Code Playgroud)

c++ r matrix rcpp

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

BackgroundColor Items ComboBox WPF

我正在做一个WPF,并有一个comboBox,其中包含计算机上可用端口的列表.我想改变这些物品的颜色.

我的comboBox是这些:

<ComboBox HorizontalAlignment="Left" Margin="445,0,0,0" VerticalAlignment="Top"     Width="120" Loaded="ComboBoxLoaded" SelectionChanged="ComboBoxSelectionChanged" Grid.Column="1" Background="#849096" Foreground="White"/>
Run Code Online (Sandbox Code Playgroud)

这些是加载它的方法:

private void ComboBoxLoaded(object sender, RoutedEventArgs e)
    {
        string [] portsList = PrintPorts();

        // get the ComboBox reference
        var comboBox = sender as ComboBox;

        // assign the ItemsSource to the List
        comboBox.ItemsSource = portsList;

        // make the first item selected
        comboBox.SelectedIndex = 0;
    }
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西,但没有任何作用.有人知道怎么做吗?谢谢!!

.net c# xaml visual-studio

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

c++ shared_ptr from char*to void*

I am trying to pass a char * string to a function which will execute inside a thread. The function has the following prototype:

void f(void *ptr);
Run Code Online (Sandbox Code Playgroud)

The thread allocation is made by a function similar to the following:

void append_task(std::function<void(void *)> &f, void *data);
Run Code Online (Sandbox Code Playgroud)

I want to allocate a string that will be used inside the thread. Right Now I have this:

string name = "random string";
char *str = new char[name.length()];
strcpy(str, name.c_str());
append_task(f, static_cast<void *>(str));
Run Code Online (Sandbox Code Playgroud)

我想放弃手动分配和释放内存的义务.我怎么能这样做std::shared_ptr …

c++ multithreading

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

迭代std :: map vs std :: unordered_map的值类型的区别

以下程序迭代unordered_map尝试找到最佳元素但不完全返回预期结果:

#include <iostream>
#include <unordered_map>
using namespace std;

struct Item
{
    int val;
};

int main() {
    unordered_map<int, Item> itemMap;
    itemMap[0] = {0};
    itemMap[1] = {1};
    itemMap[2] = {2};
    itemMap[3] = {3};
    const Item* bestItem = nullptr;
    int bestVal = -1;
    for (const pair<int, Item>& item : itemMap)
    {
        if (item.second.val > bestVal)
        {
            bestVal = item.second.val;
            bestItem = &item.second;
        }
    }
    cout << "best val: " << bestVal << " best item: " << bestItem->val;
    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11

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

是否有一种简单的方法可以关闭多个手柄?

有没有办法在C++中关闭多个句柄到线程而不必单独关闭它们?我有几个带有句柄的线程,我已经存储在一个数组中,所以我可以WaitForMultipleObjects()在关闭它们之前使用它.我知道我可以调用CloseHandle数组中的每个句柄(通过循环或只是一个一个),但我想知道是否有一种简单的方法来关闭它们.比如可能CloseHandle在阵列本身上运行(如果那样可行)?或者是否存在一种方法CloseMultipleHandles()?提前致谢.

c++ winapi multithreading handles

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

不同文件中同一命名空间中2个不同枚举中的相同标识符

我在两个不同的文件中使用相同的命名空间.在那些文件中,我有2枚枚举.我需要在这些枚举中使用相同的标识符.有办法吗?

在第一个文件first.h

...
namespace foo
{
 enum direction
 {
   NONE, 
   RIGHT    
   ...
 }
}

In second file
second.h

...
namespace foo
{
 enum reverse
 {
   NONE,  
   LEFT,   
   ...
 }
}
Run Code Online (Sandbox Code Playgroud)

这是一个示例代码.我需要重用"NONE".我需要visual studio 2008的代码.(不能使用:枚举类)

c++

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