我正在使用platform/invoke并且我正在尝试将浮点数LPSTR和int编组到c ++函数中并且我得到以下错误:调用PInvoke函数'Game!Game.Graphics :: CreateModel'使堆栈失衡.这很可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.这是我的c#代码:
public struct Graphics
{
[DllImport(@"Graphics.dll", EntryPoint = "StartGL")]
public static extern void StartGL();
[DllImport(@"Graphics.dll", EntryPoint = "CreateModel")]
public static extern void CreateModel([MarshalAs(UnmanagedType.LPStr)]string ModelPath, [MarshalAs(UnmanagedType.LPStr)]string TexturePath,float xposh, float yposh, float zposh, float rotAngleh, float xroth, float yroth, float zroth);
[DllImport(@"Graphics.dll", EntryPoint = "rotateModel")]
public static extern void rotateModel(int id,float rotAngle,float x, float y, float z);
}
class Program
{
static void Main(string[] args)
{
OpenGL();
}
static void OpenGL()
{
Graphics.CreateModel("box.obj","asd.png",0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); …Run Code Online (Sandbox Code Playgroud) 关于如何从C++ 11将C风格的整数数组转换为新的std :: array,我想知道并且没有找到任何建议
我喜欢std :: array并且使用它很多,无论如何,如果我从二进制文件中读取数据,我会努力复制值.使用fread时有没有更简单的方法?
FILE* fp; // initalize not important here
int arr[12];
std::array<int,12> stdarr;
fread(arr, sizeof(int), 12, fp);
for(int i=0; i < 12; i++)
stdarr[i] = arr[i];
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码编写自定义异常类:
#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <iomanip>
#include <algorithm>
class MyException : public std::exception {
public:
MyException();
explicit MyException(std::string message);
MyException(std::string source, std::string message);
MyException(int code, std::string source, std::string message);
const char *what() const throw();
private:
int exceptionCode;
std::string exceptionSource;
std::string exceptionMessage;
};
MyException::MyException() :
exceptionCode(0),
exceptionSource ("No source."),
exceptionMessage ("No message.") {}
MyException::MyException(std::string message) :
exceptionCode(0),
exceptionSource ("No source."),
exceptionMessage (std::move(message)) {}
MyException::MyException(std::string source, std::string message) :
exceptionCode(0),
exceptionSource (std::move(source)),
exceptionMessage (std::move(message)) {}
MyException::MyException(int code, …Run Code Online (Sandbox Code Playgroud) 所以我必须为我的大学课做一个简单的程序,其中包括一个fraction带分子和分母的结构,ints以及一个is_correct()检查两个条件的函数:分母!=0和<分母.如果其中两个是真的,那么我应该返回true,否则false.我有义务使用?:操作员.所以这是我的代码:
struct fraction {int n,d;
bool is_correct(){
d!=0?(return n<d?true:false):return false;
};
};
Run Code Online (Sandbox Code Playgroud)
我的意思是,我想我可以使用一个if条件d!=0,但我必须只使用?:,并g++给我:expected primary-expression before 'return'
我有这个
double a = 4.0;
double b = 2.0;
double g = a + b;
std::cout << g;
Run Code Online (Sandbox Code Playgroud)
我到了6,但我想得到6.0.
我怎样才能做到这一点
我在一个在线测试中遇到了这个问题.任务是改变这个程序以摆脱编译错误.
#include<iostream>
#include<iomanip>
class Vehicle
{
public:
static Car* createCar()
{
return new Car;
}
class Car
{
public:
string name;
};
private:
int seats;
};
void useVehicle()
{
Vehicle::Car *c = Vehicle::createCar();
c->name = "BMW";
}
int main(int argc, char *argv[])
{
useVehicle();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译错误如下:
error: ‘Car’ does not name a type
error: ‘string’ does not name a type
在功能中void useVehicle():
error: ‘createCar’ is not a member of ‘Vehicle’
我怎么做对了?我尝试了一些但无法解决这些错误.
我已经用可变参数模板声明了一个简单的函数。
template<typename ...Args>
void Log(const LogLevel level, const char * format, Args ...args);
Run Code Online (Sandbox Code Playgroud)
在以下列方式调用它时 -
Log(LogLevel::debug,
R"(starting x, %d pending call for "%s" with param "%s")",
id, first.c_str(),
second.c_str())
Run Code Online (Sandbox Code Playgroud)
其中变量类型是:id( unsigned int), first( std::string) , second( std::string)
我收到以下错误:
Error LNK2001 unresolved external symbol "public: void __cdecl Log<unsigned int,char const *,char const *>(enum LogLevel,char const *,unsigned int,char const *,char const *)"
Run Code Online (Sandbox Code Playgroud)
当我unsigned int从函数调用中删除参数时 - 错误消失了。AFAIK 可变参数模板确实支持不同的类型......那我错过了什么?
我使用<<变量执行了一个简单的位操作以在result第 32 个位置设置位..
result = result | (1<<31);
这应该给出输出为(二进制):
0000000000000000000000000000000010000000000000000000000000000000
和十进制:
2147483648
但这是输出(二进制):
1111111111111111111111111111111110000000000000000000000000000000
和十进制:
-2147483648
这是代码:
#include <bitset>
#include <iostream>
int main() {
int64_t result = 0;
result = result | (1 << 31);
std::bitset<64> x(result);
std::cout << x <<std:: endl;
std::cout << result << std:: endl;
}
Run Code Online (Sandbox Code Playgroud)
请帮我找出错误
我知道通过更改trig函数名称我摆脱了segfault.但是,我并不完全清楚为什么.我的猜测是我在我的实现中得到了递归调用.有人可以在这里给出严格的解释吗?
此外,我愿意保持这种方式,因此我想知道应该添加/修改什么来保留我的功能名称.
PS:我知道这是一个愚蠢的问题,只是在这里开始使用c ++,所以请耐心等待.:)
码:
#include <iostream>
#include <string>
#include <cmath>
#define _USE_MATH_DEFINES
using namespace std;
/*
Represents a calculator with various functions.
*/
class Calc {
public:
//arithmetic functions
static double sum(double a, double b){ return a+b; };
static double subtract(double a, double b){ return a-b;};
static double multiply(double a, double b){ return a*b; };
static double divide(double a, double b){ if (b == 0) return b; return a/b; };
static double avg(double a, double b){ return (a+b)/2; }; …Run Code Online (Sandbox Code Playgroud) 我有一些文件类型为"文件"我的意思是代替"资源\ sample.txt",他们的名字是"资源\样本"现在我想用c ++读取它们并将它们存储在一个字符串中.这是我的代码:
std::stringstream textBuffer;
std::fstream fileHandle;
std::string content
fileHandle.open("Resource\sample", std::ios::in);
textBuffer << fileHandle.rdbuf();
content = textBuffer.str();
fileHandle.close();
Run Code Online (Sandbox Code Playgroud)
但是当我编译它时,"content"变量的值等于"".帮助我.提前致谢.请注意,唯一的问题是文件没有任何扩展名,例如.txt或.dat或.bin.