我正在为一个类的卡片程序工作,我遇到了一个问题,编译器告诉我,当它们存在时,事物没有在范围内声明,并且有些事情根本没有被声明.这是代码:
Card.h:
#ifndef _CARD_H
#define _CARD_H
#include <iostream>
#include <string>
using namespace std;
enum RANK{Joker, Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}
enum SUIT{Clubs, Diamonds, Hearts, Spades}
class Card
{
private:
//Rank and Suit variables for all cards
int rank;
int suit;
public:
//Constructors
Card();
Card(int r, int s);
//Getters
int getRank();
int getSuit();
//Setters
void setRank(int r);
void setSuit(int s);
//toString
string toString();
};
#endif
Run Code Online (Sandbox Code Playgroud)
Card.cpp:
#ifndef _CARD_H
#define _CARD_H
#include …Run Code Online (Sandbox Code Playgroud) 调试器告诉我我没有使用我的变量,但它也没有声明。这里发生了什么?if 语句有自己的作用域吗?不知何故,固定长度的数组似乎不在 if 块内的同一范围内。
我的最小例子
#include <stdio.h>
#include <stdlib.h>
void nullarray(int start,int end, char array[]){
if(start<end) // TODO used to be <=
{
array[start]='0';
nullarray(++start,end,array);
}else{array[start]='\0';}
}
int main()
{
int commaindex2=-1;
int mostdecimaldigits=6;
if(commaindex2==-1){
char decimalnum2[1];decimalnum2[0]='0';
}
else{
char decimalnum2[mostdecimaldigits]; // get enought store incl filling up zeros
nullarray(0,mostdecimaldigits,decimalnum2); // write zeros to array
}
printf("%s", decimalnum2);
}
Run Code Online (Sandbox Code Playgroud)
调试器输出
||=== Build: Debug in test4 (compiler: GNU GCC Compiler) ===|
D:\main.c||In function 'main':|
D:\main.c|20|warning: variable 'decimalnum2' set but not used …Run Code Online (Sandbox Code Playgroud) 输出为15(在f中,x为10,y为7),具有以下内容:
var x = 5;
function f(y) { return (x + y) - 2};
function g(h) { var x = 7; return h(x) };
{ var x = 10; z = g(f); console.log(z) };
Run Code Online (Sandbox Code Playgroud)
为什么x取第4行而不是第1行的值(为什么不是第3行)?
作为一名生物学学生,我正在尝试扩展我的编程知识,并且遇到了Perl的问题.
我正在尝试创建一个程序,生成随机DNA字符串并对生成的数据执行分析工作.
在程序的第一部分,我能够打印出存储在数组中的字符串,但第二部分我无法检索除数组中的一个元素之外的所有字符串.
这可能是Perl范围规则的一部分吗?
#!usr/bin/perl
# generate a random DNA strings and print it to file specified by the user.
$largearray[0] = 0;
print "How many nucleotides for the string?\n";
$n = <>;
$mylong = $n;
print "how many strings?\n";
$numstrings = <>;
# @largearray =();
$j = 0;
while ( $j < $numstrings ) {
$numstring = ''; # start with the empty string;
$dnastring = '';
$i = 0;
while ( $i < $n ) {
$numstring = …Run Code Online (Sandbox Code Playgroud) 我不太清楚为什么数据框对象不会更新
d <- data.frame(titi=c(0))
(function(dataset) {
dataset[["toto"]] <- 1;
print(names(dataset)) #has "toto" and "titi"
})(d)
print(names(d)) # no has "toto", only "titi"
Run Code Online (Sandbox Code Playgroud)
这里发生了什么 ?
我有一个解决方法,因为在我的代码中我也捕获变量并更新捕获的<<-,但我想知道机制.
我知道一般的变异等危险.我只是不明白这里的机制.
编辑
虽然这似乎是一个共识,这是一个语言级别的功能,我不遵循这个论点,好像我使用一个紧密的结构,数据表,它可以变异:
d <- data.table(titi=c(0))
(function(dataset) {
dataset[,toto:=1]
print(names(dataset)) #"titi" "toto"
})(d)
print(names(d)) #"titi" "toto"
Run Code Online (Sandbox Code Playgroud)