我很难理解为什么类可以具有自身的不完整类型静态成员。
例如在下面的代码中。为什么允许在类A内拥有自身的不完整类型静态成员,但是当类类型B的全局静态变量是不完整类型时却出现错误?
class B {
public:
B(int a) {
}
};
static B test2; //error
class A {
public:
static A test; //accepted
A(int d) {
}
};
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释在后台给全局静态变量带来错误的原因是什么,为什么A类内部的静态变量会被接受?
使用命令通过 brew 安装 glfw 后,我无法创建版本超过 2.1 的 GLFW 窗口brew install glfw。
基本上我的问题是这段代码有效:
import qualified Graphics.UI.GLFW as GLFW
configAndCreateWindow :: IO (Maybe GLFW.Window)
configAndCreateWindow = do
GLFW.windowHint (GLFW.WindowHint'ContextVersionMajor 2)
GLFW.windowHint (GLFW.WindowHint'ContextVersionMinor 1)
GLFW.createWindow 100 100 "test" Nothing Nothing
main :: IO ()
main = do
GLFW.init
maybeWindow <- configAndCreateWindow
case maybeWindow of
Nothing -> putStrLn "Failure :("
Just _ -> putStrLn "Success!"
Run Code Online (Sandbox Code Playgroud)
但如果我改变
GLFW.windowHint (GLFW.WindowHint'ContextVersionMajor 2)
GLFW.windowHint (GLFW.WindowHint'ContextVersionMinor 1)
Run Code Online (Sandbox Code Playgroud)
到
GLFW.windowHint (GLFW.WindowHint'ContextVersionMajor 3)
GLFW.windowHint (GLFW.WindowHint'ContextVersionMinor 3)
Run Code Online (Sandbox Code Playgroud)
它坏了。
为了确保它与 GLFW-b …
在浏览 Yesod Book 的示例时,我遇到了以下代码片段的问题:
\n{-# LANGUAGE OverloadedStrings #-}\n{-# LANGUAGE QuasiQuotes #-}\n{-# LANGUAGE TemplateHaskell #-}\n{-# LANGUAGE TypeFamilies #-}\n{-# LANGUAGE ViewPatterns #-}\nimport Data.Text (Text)\nimport qualified Data.Text as T\nimport Yesod\n\ndata App = App\ninstance Yesod App\n\nmkYesod "App" [parseRoutes|\n/person/#Text PersonR GET\n/year/#Integer/month/#Text/day/#Int DateR\n/wiki/*Texts WikiR GET\n|]\n\ngetPersonR :: Text -> Handler Html\ngetPersonR name = defaultLayout [whamlet|<h1>Hello #{name}!|]\n\nhandleDateR :: Integer -> Text -> Int -> Handler Text -- text/plain\nhandleDateR year month day =\n return $\n T.concat [month, " ", T.pack $ show day, ", ", T.pack $ show …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void pointerOfPointer(struct node **reference)
{
struct node *temporary = malloc(sizeof(struct node));
temporary->data = 100;
temporary->next = 0;
printf("before: temporary->data %d\n", temporary->data);
temporary = *reference;
printf("after: temporary->data %d\n", temporary->data);
}
int main()
{
struct node *linkedlist = malloc(sizeof(struct node));
linkedlist->data = 15;
linkedlist->next = 0;
pointerOfPointer(&linkedlist);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何在不将 *reference 地址复制到临时局部变量的情况下访问 pointerOfPointer 函数中指向结构指针的指针?所以最后我可以直接使用operator -> 访问引用变量数据,比如reference->data?
有一个gnu coreutils程序'tee'和一个linux(/ libc?)系统调用'tee'.程序T恤不会打电话给发球台.相反,它'读'然后'fwrite'(当然是多次).
为什么不开球呢?我认为这是为了性能或可移植性,但我找不到答案.
后续问题,为什么它'读'和'fwrite',而不是'read'/'write'或'fread'/'fwrite'?
当一个结构具有非常量成员而另一个具有常量成员时,从一种结构转换为另一种结构(两者具有相同的形状)是否安全?该代码演示了我正在尝试做的事情..
#include <stdio.h>
#include <stdlib.h>
struct nonConst {
int value;
struct nonConst * next;
};
struct Const {
const int value;
struct nonConst * const next;
};
int main (int argc, char ** argv) {
struct nonConst * nc = (struct nonConst*)malloc(sizeof(struct nonConst));
nc->next = NULL;
nc->value = 8888;
struct Const * c = (struct Const*)nc;/*cast the non-const members to const members*/
fprintf(stdout, "%d\n", c->value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上述安全(或在某些情况下安全)还是我会遇到问题?
只要其绝对值大于0.001,该函数应返回该值.
但是,我的代码似乎有一些类型错误,我只是不知道如何修复.我已经尝试将所有类型更改为Double,但它仍然无效.
fac :: Int -> Int
fac n = if (n == 0) then 1 else n * fac (n-1)
cos :: Double -> Double
cos x = sum [cos| k <- [0..],
let cos = (-1) * (x^(2*k) `div` fac (2*k)) ,
abs (cos) > 0.001]
Run Code Online (Sandbox Code Playgroud)
这是错误:
•无法将预期类型'Double'与实际类型'Int'匹配
•在'div'的第二个参数中,即'fac(2*k)'
I'm not sure what I'm not handling. Suppose I have a function, that converts an integer to a string. Call it converter.
现在,要将位置整数转换为字符串,我只需调用converter。要将负整数转换为字符串,请附加-到converter调用中。
这是我的代码:
converter :: Integer -> String
converter x
| x == 0 = "0"
| x == 1 = "1"
| x == 2 = "2"
| x == 3 = "3"
| x == 4 = "4"
| x == 5 = "5"
| x == 6 = "6" …Run Code Online (Sandbox Code Playgroud) haskell pattern-matching guard-clause non-exhaustive-patterns
我有一个像下面这样的变量。
1.1.0.1580308800316
有没有办法将 1.1.0 作为单独的变量,将 1580308800316 作为单独的变量?喜欢var1=1.1.0 and var2=1580308800316
我尝试了可以找到的以下代码行。
echo 1.1.0.1580308800316 | fold -w6
但这并没有给我带来我想要的结果。
``