小编S.M*_*.M.的帖子

写入二进制文件

我正在尝试在 Fortran 90 中编写 STL 二进制文件。该文件具有以下格式

HEADER:80 字节 ASCII 标头 - TITLE

4 字节无符号长整型,NO。刻面数量

每个方面的格式:

法向量,3 个浮点值,每个值 4 字节;

顶点 1 XYZ 坐标,3 个浮点值,每个值 4 字节;

顶点 2 XYZ 坐标,3 个浮点值,每个值 4 字节;

顶点 3 个 XYZ 坐标,3 个浮点值,每个值 4 字节;

2 个字节的无符号整数,应该为零;

我正在尝试创建一个未格式化的文件来写入相关信息,但在定义正确的记录长度时遇到问题。假设我有N个facet,我使用以下命令打开并写入信息

open(unit = 1, status = 'replace', iostat = ioerror, format = 'unformatted', access = 'direct', recl = 84 + N * 50, file = 'c:\temp\test.stl')
Run Code Online (Sandbox Code Playgroud)

我可以发出第一个写入语句来写出标头信息,然后发出第二个写入语句(在 do 循环内)来写出方面信息吗?

如果是这样,由于我有不同记录长度的标头和方面信息,因此每个写入语句需要的记录号是多少。

write(1,rec=?), *header information*
do,i=1,N,1
   write(1,rec=?), *facet information* …
Run Code Online (Sandbox Code Playgroud)

fortran binaryfiles stl-format

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

为什么 clang 会发出警告:文件末尾未终止的 '#pragma pack (push, …)' ?

我在启用了clangd 的vscode 中创建了一个 main.cpp ,并将以下代码放入其中。

\n

clangd用警告消息警告第一行:

\n
\n

警告:文件末尾未终止的 \xe2\x80\x98#pragma pack (push, \xe2\x80\xa6)\xe2\x80\x99

\n
\n

main.cpp的全部内容:

\n
#pragma pack(push) // warning on this line\n#pragma pack(1)\nstruct A\n{\n    int   a;\n    short b;\n    char  c;\n};\n#pragma pack(pop)\n
Run Code Online (Sandbox Code Playgroud)\n

另请参阅: https: //releases.llvm.org/13.0.0/tools/clang/docs/DiagnosticsReference.html#wpragma-pack

\n

我认为这是 的一个非常常见的用法#pragma pack(push),我不明白为什么会生成警告。

\n

对我来说更奇怪的是,如果我在第一行之前添加分号,警告就会消失。

\n
;                  // Add a semicolon\n#pragma pack(push) // The warning disappears\n#pragma pack(1)\nstruct A\n{\n    int   a;\n    short b;\n    char  c;\n};\n#pragma pack(pop)\n
Run Code Online (Sandbox Code Playgroud)\n

背后的原因是什么?

\n

c++ clangd

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

如何在从 Github 下载的 CMake 中包含 Google Mock

在 google test 的快速入门(https://google.github.io/googletest/quickstart-cmake.html)中,我找到了以下代码来从 Github 下载 google test 依赖项:

cmake_minimum_required(VERSION 3.14)
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)

FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
  hello_test
  hello_test.cc
)
target_link_libraries(
  hello_test
  gtest_main
)

include(GoogleTest)
gtest_discover_tests(hello_test)
Run Code Online (Sandbox Code Playgroud)

这适用于谷歌测试,并且在测试文件 hello_test.cc 中我可以#include "gtest/gtest.h"成功包含。

但是,我还想包括 Gmock:#include "gmock/gmock.h"但它找不到它。

如何包含 gmock 下载 gtest 等依赖项?

c++ cmake googletest googlemock

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

如何将字符串向量传递给具有 C 风格指针 C 字符串参数的函数

我需要调用下面的 getList 函数,该函数来自我无法更改的库。

#include <iostream>
#include <vector>
#include <string>

//function already exists in a separate library, can't be changed
void getList(const char* list[], int count){};

int main()
{
    std::vector<std::string> vectorOfStrings = {"123" , "abc", "def", "456"};
    
    //call getList and pass vectorOfStrings and vectorOfStrings.size()
    getList(/*convert std::vector<std::string> to const char** */, vectorOfStrings.size());
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我已经在这里问过类似的问题并得到了答案,但我认为可能有一种 C++ 方法可以做到这一点。

c++ pointers

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

GMock:如何返回由 EXPECT_CALL() 定义的函数指针

我正在使用一个将函数指针作为void*. 我想要一个模拟返回一个函数指针,并且我想要就地定义该函数(如 lambda;它不起作用,如下所示)。

下面显示了一个最小的工作示例。

#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace std;
using namespace testing;

class Original
{
public:
    typedef int(*fptr)();

    void Func() 
    {
        void* f = Func2();
        fptr func = reinterpret_cast<fptr>(f);
        if (func) {
            int i = func();
            if (i == 1) {
                //do something
            } else if (i == 3) {
                //NOTE my unit test should test this decision branch
            }
        }
    }

    static int Func3() {return 1;}

    virtual void* Func2() {return (void*)&Func3;}
};

class MyMock : …
Run Code Online (Sandbox Code Playgroud)

c++ lambda googletest googlemock

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

为什么在 C++ 中插入向量有效?

https://docs.microsoft.com/en-us/cpp/cpp/value-types-modern-cpp?view=vs-2019,我们有:

#include <set>
#include <vector>
#include <string>
using namespace std;

//...
set<widget> LoadHugeData() {
    set<widget> ret;
    // ... load data from disk and populate ret
    return ret;
}
//...
widgets = LoadHugeData();   // efficient, no deep copy

vector<string> v = IfIHadAMillionStrings();
v.insert( begin(v)+v.size()/2, "scott" );   // efficient, no deep copy-shuffle
v.insert( begin(v)+v.size()/2, "Andrei" );  // (just 1M ptr/len assignments)
//...
HugeMatrix operator+(const HugeMatrix& , const HugeMatrix& );
HugeMatrix operator+(const HugeMatrix& ,       HugeMatrix&&);
HugeMatrix operator+(      HugeMatrix&&, const HugeMatrix& ); …
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么在我的程序中调用了两次 operator()?

#include <unordered_set>
#include <stdio.h>

int hcount=0;

struct A{
    int i=0;

    A(){}
    A(const A&a) :i(a.i){}

    A(int const & i):i(i) {        
        printf("A ctor i=%d\n", i);
    }

    A&operator=(A &&a){
        this->i= a.i;
        return (*this);
    }
    bool operator==(A const &rhs) const {
        printf("A optor== i=%d\n", this->i);
        return rhs.i == this->i;
    }
};

namespace std{
    template<>
    struct hash<A> { 
        hash() {            
            hcount=0;
        }
        hash(int t) {
            hcount=t;
        }
        std::size_t operator()(A const &a) const {
            ++hcount;
            printf("hash: hcount=%d a.i=%d\n", hcount, a.i);
            return a.i;
        };
    };
}

int …
Run Code Online (Sandbox Code Playgroud)

c++ std

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

使共享指针指向变量

刚刚开始使用共享指针并尝试了这样的示例程序:

int x = 10;
shared_ptr<int> ptr = make_shared<int>(x);
*ptr = 11;
cout<< x << " " << *ptr;
Run Code Online (Sandbox Code Playgroud)

结果是 10 11 这对我来说没有意义,因为 ptr 指向 x,因此 x 的值应该更改为 11。有人可以在这里解释一下吗?另外,当指向 x 的共享指针更改时,我需要更改 x 的值。请让我知道如何才能实现这一目标。提前致谢。

c++ pointers shared-ptr c++11

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

将赋值运算符从基类引入派生对象(C++11 之前的版本)

我有一个与此类似的代码:

template <typename T>
struct B
{
    B &operator =(const T &) { return *this; }
};

struct D : B<int> {};

int main()
{
    D d;
    d = 0;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

哪个失败了:

error: no viable overloaded '='
   d = 0;
   ~ ^ ~
note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'int' to 'const D' for 1st argument
struct D : B<int> {};
       ^
note: candidate function (the implicit move assignment operator) …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance operator-overloading c++98

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

配置 g++ 以使用 wxwidget 库构建 c++

我一直在尝试在 Linux 中使用 Wxwidget 编译并运行一个简单的 C++ 程序,但是当我构建它时,这就是我尝试构建时得到的结果:

Executing task: g++ -c $(find /home/sopheak/Documents/WXWIDGET/ -type f -iregex '.*\.cpp') -g -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -DWX_PRECOMP -fno-strict-aliasing -pthread -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/** -Iusr/include/** -I/usr/include/gtk-3.0/** -I/usr/include/at-spi2-atk/2.0/** -I/usr/include/at-spi-2.0/** -I/usr/include/dbus-1.0/** -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include/** -I/usr/include/gio-unix-2.0/** -I/usr/include/cairo/** -I/usr/include/pango-1.0/** -I/usr/include/fribidi/** -I/usr/include/harfbuzz/** -I/usr/include/atk-1.0/** -I/usr/include/pixman-1/** -I/usr/include/uuid/** -I/usr/include/freetype2/** -I/usr/include/libpng16/** -I/usr/include/gdk-pixbuf-2.0/** -I/usr/include/libmount/** -I/usr/include/blkid/** -I/usr/include/glib-2.0/** -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/** -I/usr/include/gtk-3.0/unix-print/** -Wall
  
zsh:1: no matches found: -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/**
The terminal process "zsh '-c', 'g++ -c $(find /home/sopheak/Documents/WXWIDGET/ -type f -iregex '.*\.cpp') -g -D__WXGTK__ -D_FILE_OFFSET_BITS=64 -DWX_PRECOMP -fno-strict-aliasing -pthread -I/usr/local/lib/wx/include/gtk3-unicode-static-3.1/** -Iusr/include/** -I/usr/include/gtk-3.0/** -I/usr/include/at-spi2-atk/2.0/** -I/usr/include/at-spi-2.0/** -I/usr/include/dbus-1.0/** -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include/** -I/usr/include/gio-unix-2.0/** -I/usr/include/cairo/** …
Run Code Online (Sandbox Code Playgroud)

c++ linux wxwidgets visual-studio-code

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