小编Joh*_*nes的帖子

SonarQube无法在CS代码中检测到重复

我正在测试SonarQube,并且此代码存在于测试项目中:

public class Widgetor
{
    internal static int SelectValue(int ret)
    {
        switch(ret)
        {
            case 0: return 1;
            case 4: return 7;
            case -1: return 2;
            case 2: return -1;
            default: return 0;
        }
    }
    internal static int SelectValue_Copy(int ret)
    {
        switch (ret)
        {
            case 0: return 1;
            case 4: return 7;
            case -1: return 2;
            case 2: return -1;
            default: return 0;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用MSBuild Runner和MSBuil 14运行SonarQube版本5.6。

对于如何配置我的项目进行代码复制检测,我找不到任何帮助。据我所知,这应该是开箱即用的一部分。项目概述显示“ 0%”重复的代码。

我发现这个答案并没有真正告诉我任何事情。

c# msbuild sonarqube

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

如何通过C90中的函数传递va_list

我想将va_list传递给另一个函数.这是我想要做的一个例子:

void my_printf_1(char* string, ...) {
    va_list ap;
    va_start(ap, string);
    printf(string, ap);
    va_end(ap);
}

void my_printf_2(char* string, ...) {
    va_list ap;
    va_start(ap, string);
    printf(string, *ap);
    va_end(ap);
}

int main(void) {
    my_printf_1("Hello %i\n", 12); //Shows me the pointer
    my_printf_1("Hello \n"); //Runtime Error - too many arguments
    my_printf_2("Hello %i\n", 12); //Displays correctly because 12 < char
    my_printf_2("Hello %i\n", 500); //Displays incorrectly  because 500 > char
    my_printf_2("Hello %i %i\n", 12, 12); //Displays 12, then crashes Runtime Error not enough arguments
    my_printf_2("Hello \n"); //Runtime …
Run Code Online (Sandbox Code Playgroud)

c variadic-functions cvi c89

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

如何更改嵌套构造函数的调用顺序(抽象父项之前的子项)

下面的代码抛出异常,因为在子构造函数之前调用了抽象构造函数.

我需要提供一个抽象类来封装程序不同部分的一些逻辑.但是,我还需要检查抽象成员是否在创建后正确初始化,而子类没有对此有任何影响.

下面的编译示例应该说明我的问题.

using System;

namespace Stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = new Thing(5);
            var y = new Child(x);
        }
    }

    class Child : AbstractParent
    {
        Thing childthing;

        public Child(Thing provided) : base(){
            childthing = provided;
        }

        public override void Initialise(){
            //Exception is thrown here - childthing is still null
            parentthing = childthing.Add(1);
        }
    }

    abstract class AbstractParent
    {
        protected Thing parentthing;

        public AbstractParent(){
            Initialise();
            AssertThingyNotNull();
        }

        private void AssertThingyNotNull(){
            if (parentthing == …
Run Code Online (Sandbox Code Playgroud)

c# polymorphism abstract

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

将DataSet放入工作表的最快方法

需要将具有16000 x 12个条目的相当higeisch数据集转储到工作表中.

我现在使用以下功能:

        for (int r = 0; r < dt.Rows.Count; ++r)
        {
            for (int c = 0; c < dt.Columns.Count; ++c)
            {
                worksheet.Cells[c + 1][r + 1] = dt.Rows[r][c].ToString();
            }
        }
Run Code Online (Sandbox Code Playgroud)

我把这个例子改成了中心部分


以下是我在阅读Dave Zych的建议后实施的内容.这非常有效.

    private static void AppendWorkSheet(Excel.Workbook workbook, DataSet data, String tableName)
    {
        Excel.Worksheet worksheet;
        if (UsedSheets == 0) worksheet = workbook.Worksheets[1];
        else worksheet = workbook.Worksheets.Add();
        UsedSheets++;
        DataTable dt = data.Tables[0];
        var valuesArray = new object[dt.Rows.Count, dt.Columns.Count];

        for (int r = 0; r < dt.Rows.Count; …
Run Code Online (Sandbox Code Playgroud)

c# excel com-interop worksheet dataset

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

Casting HexNumber as character to string

I need to process a numeral as a string.

My value is 0x28 and this is the ascii code for '('.

I need to assign this to a string.

The following lines do this.

char c = (char)0x28;
string s = c.ToString();

string s2 = ((char)0x28).ToString();
Run Code Online (Sandbox Code Playgroud)

My usecase is a function that only accepts strings. My call ends up looking cluttered:

someCall( ((char)0x28).ToString() );
Run Code Online (Sandbox Code Playgroud)

Is there a way of simplifying this and make it more readable without writing '(' …

c# casting

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

c ++中'char()'和'char'有什么区别?

在我写boost::spirit::qi规则时,我偶然发现了这一点.我写了一个不正确的规则声明,我通过添加括号来修复.在我看来,我不知道为什么会有所作为.

char和之间有什么区别char()

这是一个最小的例子,显示了它的相关性.

测试A并且B是等效的.测试C编译但未通过测试.测试D已注释掉,不会使用以下消息进行编译:C2440: 'static_cast' : cannot convert from 'skipper_type' to 'char'.(类型稍微复杂一点,错误就是C2664)

测试示例仅用于说明charvs 的使用在哪些方面char()有所不同.我的问题是两者之间的区别是什么.

#include <iostream>
#include <string>
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>
#include <boost/spirit/include/qi.hpp>

using std::string;
namespace qi = boost::spirit::qi;
using Iterator = std::string::iterator;
using Skipper = qi::space_type;

Skipper skipper = qi::space;

void CHECK_ITERATOR(Iterator p, Iterator end)
{
    if (p != end)
    {
        BOOST_CHECK_MESSAGE(p == end, "Remaining: " …
Run Code Online (Sandbox Code Playgroud)

c++

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

更改 gpg2.exe 的输出语言

我正在自动化一个过程,并为此使用 GPG2.exe。因为我需要解析控制台输出 - 可能来自不同的系统,我需要将语言设置为受控值。

我正在遵循手册中的说明,其中指出

LANGUAGE
除了被 GNU 使用外,它还在 W32 版本中用于覆盖通过注册表完成的语言选择。如果使用并设置为有效且可用的语言名称 (langid),则带有翻译的文件将从 gpgdir/gnupg.nls/langid.mo 加载。这里 gpgdir 是加载 gpg 二进制文件的目录。如果无法加载,则尝试注册表并作为最后的手段使用本机 Windows 语言环境系统。

我发现了2011 年的一个线程,它更详细地介绍了这个问题,但这实际上可能涉及不同的版本。

我创建了一个批处理文件用于手动测试。

@echo off
REM C is meant to display untranslated messages according to one internet source
set LANGUAGE="C"
call "C:\Program Files (x86)\GNU\GnuPG\gpg2.exe" --version
pause
Run Code Online (Sandbox Code Playgroud)

我将输出为英语,但它仍然是德语。

在此处输入图片说明

手册说明了一些关于在某处开始“gnupg.nls”文件夹的内容。我找不到这个文件夹,这让我想知道德语是从哪里加载的。手册页中是否有错误?手册页的 pdf 版本显示与安装附带的手册页相同的内容。

有人可以对此有所了解吗?

config gnupg

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

在Windows上将filesystem :: path转换为char*

C++ 17的文件系统是基于Boost.Filesystem的.

我现在正在Windows上使用VS2017.

#include <filesystem>
namespace fs = std::experimental::filesystem;
Run Code Online (Sandbox Code Playgroud)

我遍历一个目录

for (auto& p: fs::directory_iterator("media"))
Run Code Online (Sandbox Code Playgroud)

我想将路径传递给一个将文件路径作为的函数 const char *

我在这里找到了一个关于boost文件系统的类似问题.核心差异在于C++ 17中的路径基于a value_type.哪里

value_type:文件系统的本机编码使用的字符类型:POSIX上的char,Windows上的wchar_t

所以我得到的是一个const wchar_t *字符串.

以下"适用于我":

    char file[2000];
    wcstombs(file, p.path().c_str(), 2000);
    auto image = SDL_LoadBMP(file);
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个不同的版本,因为这个实现是各种乱码(衰减数组到指针和_CRT_SECURE_NO_WARNINGS).

我正在寻找一个更漂亮的版本,可以直接从路径到const char *在Windows上使用新的C++ 17文件系统.

这是我用过的SDL2项目.

#define _CRT_SECURE_NO_WARNINGS 1
#include <SDL.h>
#include <vector>
#include <filesystem>
namespace fs = std::experimental::filesystem;

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    auto window = …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

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

扩展System.Type的"<"和">"运算符

可能重复:
使用C#扩展方法重载运算符

我如何重载这些运算符,我觉得编译器误解了.

我认为核心问题是我试图将运算符重载为类的扩展.类型类没有那些运算符,所以我觉得这样做很安全 - 但我的编译器不同意.

    public static class TypeCheck
    {
        public static Boolean ToBool(this Type t1, Type t2)
        {
            //normal extension works
            return true;
        }

        public static Boolean operator > (this Type t1, Type t2)
        {
            //TODO once it compiles
            return fasle;
        }

        public static Boolean operator < (this Type t1, Type t2)
        {
            //TODO once it compiles
            return true;
        }

    }
Run Code Online (Sandbox Code Playgroud)

为了阐明这些比较的领域细节:class A : B {},class B {}class C {} A是比其余部分大于A和大于乙但较小.因为A.IsCastableTo(B)和A.IsCastableTo(A);

c# operator-overloading

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

使用functioncall初始化字符串成员c ++

对象有一个字符串,需要构造.

#include <string>

class SDLException
{
private:
    std::string what_str;
public:
    SDLException(const std::string &msg);
    ~SDLException(void);
};
Run Code Online (Sandbox Code Playgroud)

该字符串具有隐藏的依赖关系,我需要考虑(SDL_GetError()).我可以在函数中构造字符串.但我不知道如何使用该函数的返回值来初始化字符串成员.

#include "SDLException.hpp"

#include <sstream>
#include <string>
#include <SDL.h>

static void buildSTR(const std::string &msg)
{
    std::ostringstream stream;
    stream << msg << " error: " << SDL_GetError();
    std::string str =  stream.str();
    //if i return a string here it would be out of scope when i use it
}

SDLException::SDLException(const std::string &msg)
    : what_str(/*i want to initialise this string here*/)
{}

SDLException::~SDLException(void){}
Run Code Online (Sandbox Code Playgroud)

如何what_str以最小的开销初始化成员?内容 …

c++ initialization member

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