我正在尝试在 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) 我在启用了clangd 的vscode 中创建了一个 main.cpp ,并将以下代码放入其中。
\nclangd用警告消息警告第一行:
\n\n\n警告:文件末尾未终止的 \xe2\x80\x98#pragma pack (push, \xe2\x80\xa6)\xe2\x80\x99
\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)\nRun 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; // 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)\nRun Code Online (Sandbox Code Playgroud)\n背后的原因是什么?
\n在 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 等依赖项?
我需要调用下面的 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++ 方法可以做到这一点。
我正在使用一个将函数指针作为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) 从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) #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) 刚刚开始使用共享指针并尝试了这样的示例程序:
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 的值。请让我知道如何才能实现这一目标。提前致谢。
我有一个与此类似的代码:
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) 我一直在尝试在 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++ ×9
googlemock ×2
googletest ×2
pointers ×2
binaryfiles ×1
c++11 ×1
c++98 ×1
clangd ×1
cmake ×1
fortran ×1
inheritance ×1
lambda ×1
linux ×1
shared-ptr ×1
std ×1
stl-format ×1
wxwidgets ×1