小编Ben*_*igt的帖子

在C++/CLI中通过引用传递参数,因此重新分配会影响调用者

可能这不是一个困难的问题,但我总是对如何将String类型视为Visual C++中的参数感到困惑.我有以下功能:

void function_1(String ^str_1)
{
  str_1 = gcnew String("Test");
}

void function_2()
{
  String ^str_2 = nullptr;
  function_1(str_2);
}
Run Code Online (Sandbox Code Playgroud)

在打电话之后function_1,str_2仍然等于null,但我想要实现的str_2是等于Test.那么,我怎样才能实现将内容str_1传递给function_2

谢谢你的建议.

c++-cli pass-by-reference

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

是否有C++/CLI智能指针项目(例如scoped_ptr)?

是否有C++/CLI RAII智能指针类用于包含托管类型中的本机指针?只是想知道,在我编写自己的clr_scoped_ptr值类模板之前.

我知道微软提供的:

以上两者类似于auto_ptrunique_ptr.

但所有这些都是用于处理托管的ref类实例,而不是用于释放本机对象.

c++-cli smart-pointers

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

从std :: vector中的每个结构中选择一个成员到另一个向量中

我有一个Visual Studio 2008 C++项目,我想将该结构类型的向量的一个struct元素复制到一个新的向量.例如:

struct Foo {
    int a;
    long b;
};

std::vector< Foo > v1;
std::vector< long > v2;

for( std::vector< Foo >::const_iterator it = v1.begin(); it != v1.end(); ++it )
{
    v2.push_back( it->b );
}
Run Code Online (Sandbox Code Playgroud)

有比这更好/更优雅的方式吗?

谢谢,PaulH

c++ stl

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

在C++ 11中,受保护意味着公众?

继续学习C++中的错误:基本功能受到保护 ......

C++ 11指向成员的规则有效地剥离了protected任何值的关键字,因为受保护的成员可以在不相关的类中访问而不会有任何恶意/不安全的强制转换.

以机智:

class Encapsulator
{
  protected:
    int i;
  public:
    Encapsulator(int v) : i(v) {}
};

Encapsulator f(int x) { return x + 2; }

#include <iostream>
int main(void)
{
    Encapsulator e = f(7);
    // forbidden: std::cout << e.i << std::endl; because i is protected
    // forbidden: int Encapsulator::*pi = &Encapsulator::i; because i is protected
    // forbidden: struct Gimme : Encapsulator { static int read(Encapsulator& o) { return o.i; } };

    // loophole:
    struct Gimme …
Run Code Online (Sandbox Code Playgroud)

c++ member member-access

13
推荐指数
2
解决办法
843
查看次数

C++实现中"无效指针值"转换的文档

根据C++标准,每个实现都必须记录"实现定义的行为":

1.3.11 [defns.impl.defined] 实现定义的行为

行为,对于格式良好的程序构造和正确的数据,这取决于实现和每个实现文档

读取无效指针值具有实现定义的行为(请参阅4.1左值到右值转换[conv.lval]):

如果glvalue引用的对象包含无效指针值(3.7.4.2,3.7.4.3),则行为是实现定义的.

(引自n4527草案,但是"通过无效指针值间接并将无效指针值传递给释放函数的行为具有未定义的行为.无效指针值的任何其他使用都具有实现定义的行为."已经在3.7中. 4.2 [basic.stc.dynamic.deallocation]自至少草案n3485以来的释放功能

但是,许多流行的实现并未定义此行为,许多专家将此描述为"未定义的行为".

遗漏明确文件的一个可能原因是,据我所知,"附录后的标准草案"中出现的"实施定义行为指数"中缺少对"无效指针值"的评估.

这是标准中的缺陷吗?自C++ 14以来,是否有任何公开的缺陷报告或委员会行动?

c++ pointers language-lawyer

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

对从放置new获取的指针使用operator delete的合法性

我确信这段代码应该是非法的,因为它显然不会起作用,但它似乎被C++ 0x FCD所允许.

class X { /* ... */};
void* raw = malloc(sizeof (X));
X* p = new (raw) X(); // according to the standard, the RHS is a placement-new expression
::operator delete(p); // definitely wrong, per litb's answer
delete p; // legal?  I hope not
Run Code Online (Sandbox Code Playgroud)

也许你们中的一位语言律师可以解释标准如何禁止这一点.

还有一个数组形式:

class X { /* ... */};
void* raw = malloc(sizeof (X));
X* p = new (raw) X[1]; // according to the standard, the RHS is a placement-new expression
::operator delete[](p); // definitely …
Run Code Online (Sandbox Code Playgroud)

c++ placement-new language-lawyer delete-operator

12
推荐指数
2
解决办法
1051
查看次数

模块编译:找不到asm/linkage.h文件

我正在尝试编译"hello world"内核模块的示例,在ubuntu 11.04,内核3.2.6,gcc 4.5.2和fedora 16,内核3.2.7,gcc 4.6.7上发现问题.

码:

#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");

static int __init hello_init (void)
{
printk("Hello module init\n");
return 0;
}
static void __exit hello_exit (void)
{
printk("Hello module exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
Run Code Online (Sandbox Code Playgroud)

编译:

gcc -D__KERNEL__ -I /usr/src/linux/include/ -DMODULE -Wall -O2 -c hello.c -o hello.o
Run Code Online (Sandbox Code Playgroud)

错误:

在/usr/src/linux/include/linux/kernel.h:13:0中的文件中,来自/usr/src/linux/include/linux/cache.h:4,来自/ usr/src/linux/include /linux/time.h:7,来自/usr/src/linux/include/linux/stat.h:60,来自/usr/src/linux/include/linux/module.h:10,来自hello.c: 1:/usr/src/linux/include/linux/linkage.h:5:25:致命错误:asm/linkage.h:找不到文件

然后我发现在/ usr/src/linux/include /中没有名为'asm'但'asm​​-generic'的文件夹; 所以我把'asm'软链接到'asm-generic',并编译了agail:

这次错误是:

在/usr/src/linux/include/linux/preempt.h:9:0中包含的文件中,来自/usr/src/linux/include/linux/spinlock.h:50,来自/ usr/src/linux/include /linux/seqlock.h:29,来自/usr/src/linux/include/linux/time.h:8,来自/usr/src/linux/include/linux/stat.h:60,来自/ usr/src /linux/include/linux/module.h:10,来自hello.c:1:/usr/src/linux/include/linux/thread_info.h:53:29:致命错误:asm/thread_info.h:文件没有发现

所以我意识到我错了,但为什么呢?T_T

linux kernel kernel-module linux-kernel

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

结构内部结构

我必须创建一个人,每个人都应该有一个冰箱.这是最好的方式吗?如果是这样,我做错了什么?提前致谢.

typedef struct {
 int age;
 struct FRIDGE fridge;
} PERSON;

typedef struct {
 int number;
} FRIDGE;

FRIDGE fr;
fr.number=1;

PERSON me;
me.name=1;
me.fridge = fr;
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

错误:字段'冰箱'的类型不完整

c struct typedef compiler-errors

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

如何穿越dacpac

我们希望将dbproj升级到sqlproj,以便我们可以将它指向新的SQL 2012数据库.我们现在有一个程序读取.dbschema xml文件以查找所有表和列并从中检索信息.我们使用这些数据来构建我们自己的自定义类.

新的sqlproj文件现在生成一个dacpac,我们想要进行干预以获取我们需要的数据.我写了以下内容来尝试遍历dacpac并获取我需要的信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Dac;
using Microsoft.SqlServer.Dac.Extensions;
using Microsoft.SqlServer.Dac.Model;
namespace DacPacReader
{
    class Program
    {
        static void Main(string[] args)
        {
            using (System.IO.TextWriter writter = new System.IO.StreamWriter(@"c:\temp\output.txt"))
            {
                using (TSqlModel model = new TSqlModel(@"C:\temp\Data.dacpac"))
                {
                    var allTables = model.GetObjects(DacQueryScopes.All, ModelSchema.Table);

                    foreach (var table in allTables)
                    {
                        writter.WriteLine(table.Name);
                        foreach (var column in table.GetChildren().Where(child => child.ObjectType.Name == "Column"))
                        {
                            writter.WriteLine("\t" + column.Name);
                            writter.WriteLine("\tProperties:");
                            foreach (var property in column.ObjectType.Properties)
                            {
                                writter.WriteLine("\t\t" + property.Name …
Run Code Online (Sandbox Code Playgroud)

c# sql-server dacpac

12
推荐指数
2
解决办法
3448
查看次数

OpenCV 3中的FLANN错误

我正在运行Ubuntu 14.04.我试图用openCV 3运行FLANN,但是我收到错误.

通过使用AKAZE和ORB尝试下面的所有内容,但是从我尝试使用ORB的代码.

我使用ORB来查找描述符和关键点.

  Ptr<ORB> detector = ORB::create();

  std::vector<KeyPoint> keypoints_1, keypoints_2;
  Mat descriptors_1, descriptors_2;

  detector->detectAndCompute( img_1, noArray(), keypoints_1, descriptors_1 );
  detector->detectAndCompute( img_2, noArray(), keypoints_2, descriptors_2 );
Run Code Online (Sandbox Code Playgroud)

使用ORB后,我使用以下代码查找匹配项:

  FlannBasedMatcher matcher;
  std::vector<DMatch> matches;
  matcher.match(descriptors_1, descriptors_2, matches);
Run Code Online (Sandbox Code Playgroud)

代码构建良好和一切.当我运行代码时,我收到此错误:

OpenCV Error: Unsupported format or combination of formats (type=0
) in buildIndex_, file /home/jim/opencv/modules/flann/src/miniflann.cpp, line 315
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/jim/opencv/modules/flann/src/miniflann.cpp:315: error: (-210) type=0
 in function buildIndex_

Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我为什么?这是OpenCV 3处于BETA状态的问题吗?是否有替代FLANN(BFMatcher除外)

c++ opencv orb flann

12
推荐指数
2
解决办法
9765
查看次数