说我有以下功能:
const std::string& Cat::getKittenName() const
{
Kitten* kitty = getKitty();
return kitty->getName();
}
Run Code Online (Sandbox Code Playgroud)
哪里Kitten::getName
返回一个const std::string&
如何最好地处理的情况kitty
是nullptr
哪个?我可以返回,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)
唯一的问题可能是"魔法静力学"不可用.这个解决方案有什么问题,或者有更好的方法吗?
我一直在尝试使用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) 我今天将xcode更新为7.更新后,我正在处理的项目有"覆盖成员函数但未标记为'覆盖'"的警告.由于我们的项目将"treade warning as error"设置为true.我有很多错误.
我仔细检查了"其他链接器标志"并且非常确定不包括标志"-Wsuggest-override".
由于这是一个大项目,我更喜欢禁止警告,而是在任何地方添加关键字"覆盖".
有关禁用警告的任何建议吗?
谢谢
我的图像宽度为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;
我希望动态调整大小的窗口具有列布局,以便列中的最后一项填充剩余的剩余空间.我可以通过动态计算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) 下面的代码很高兴被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 …
我是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) 我正在做一个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)
我尝试了很多东西,但没有任何作用.有人知道怎么做吗?谢谢!!
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 …
以下程序迭代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++中关闭多个句柄到线程而不必单独关闭它们?我有几个带有句柄的线程,我已经存储在一个数组中,所以我可以WaitForMultipleObjects()
在关闭它们之前使用它.我知道我可以调用CloseHandle
数组中的每个句柄(通过循环或只是一个一个),但我想知道是否有一种简单的方法来关闭它们.比如可能CloseHandle
在阵列本身上运行(如果那样可行)?或者是否存在一种方法CloseMultipleHandles()
?提前致谢.
我在两个不同的文件中使用相同的命名空间.在那些文件中,我有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的代码.(不能使用:枚举类)