我有一个3d矢量数据类型定义为3个浮点数.我明白如果我Num为我的类提供一个实例并定义普通的数学运算符,我可以在我的课上使用它们.
data Vec3 = Vec3 { x :: Float
, y :: Float
, z :: Float
} deriving (Show, Eq)
instance Num Vec3 where
(+) v1 v2 = Vec3 (x v1 + x v2) (y v1 + y v2) (z v1 + z v2)
Run Code Online (Sandbox Code Playgroud)
当我将文件加载到ghci时,我收到警告,因为我没有定义所有函数Num,这是有道理的.
Prelude> :l temp.hs
[1 of 1] Compiling Main ( temp.hs, interpreted )
temp.hs:6:10: Warning:
No explicit method or default declaration for `*'
In the instance declaration for `Num Vec3'
temp.hs:6:10: …Run Code Online (Sandbox Code Playgroud) 将静态 c++ 库链接到 ac 可执行文件时,有没有办法强制 cmake 使用 c++ 链接器?
我有一个静态库,由 2 个对象组成,一个 C++ 文件和一个该文件中函数的 C 包装器(构造函数、析构函数和打印函数),类似于此SO 答案。最后一段指出:
现在有趣的部分是确保您将所有必需的 C++ 库正确链接到更大的库中。对于 gcc(或 clang),这意味着仅使用 g++ 执行最后的链接阶段。
我可以用我的 MCVE 验证这一点。替换gcc为g++修复问题并且一切正常
$ gcc -static main.c -L. -lCPPclass -o main
./libCPPclass.a(CInt.o): In function `newCINT':
CInt.cpp:(.text+0xd): undefined reference to `operator new(unsigned long)'
CInt.cpp:(.text+0x28): undefined reference to `operator delete(void*)'
./libCPPclass.a(CInt.o): In function `delCINT':
CInt.cpp:(.text+0x5e): undefined reference to `operator delete(void*)'
./libCPPclass.a(CInt.o):(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
./libCPPclass.a(CPPclass.o): In function `CPPclass::print_success()':
CPPclass.cpp:(.text+0x26): undefined …Run Code Online (Sandbox Code Playgroud) 此处已经提出了类似的问题,但根据该问题的答案和Julia手册,以下.jl脚本应该可以正常工作.
global myVar = spzeros(10,1);
myVar[3] = 1;
function test_base()
test1();
end
function test1()
myVar = [ i > 0 ? 2 : 0 for i in myVar] #doesn't work
end
Run Code Online (Sandbox Code Playgroud)
我明确声明一个变量global,然后尝试在函数内修改它.但是,当我尝试运行函数test1()时,它表示变量未定义.
julia> VERSION
v"0.3.5"
julia> include("test.jl")
test1 (generic function with 1 method)
julia> test_base()
ERROR: myVar not defined
in test1 at /home/clifton/Julia/ca-1/test.jl:9
in test_base at /home/clifton/Julia/ca-1/test.jl:5
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的东西,如果我只是访问test1()中的变量,它确实有效,就像print(myVar);有人知道我做错了吗?