我正在为学校开发一个名为LCR的骰子游戏。我终于把程序编译好了,把游戏例程也做好了。我现在只能从我的Player类访问main中的数组。特别是Player::setChips。我可能应该使用指针,但我在这方面非常粗略,任何输入都可能是很好的。该函数正在调用fine,并且输出可以工作,但是我注释掉的行显然不工作,但是给出了我想要做的事情的想法。谢谢。
下面是我的LCR Game.cpp代码:
int main()
{
// Display Game Rules
Player::directions();
// Get # of Players
int
我试图创建一个速度、距离和时间计算器,这是尽可能有效的,并希望引用调用一个函数,使用指针根据输入变化,但不确定如何这样做。我尝试过许多不同的东西:
我最好的尝试:
// Inside class Math
double calcSpeed(double distance, double time); // These functions return a double value of the formula (non static)
double calcDistance(double speed, double time);
double calcTime(double speed, do
我试图调用一个函数,但总是收到一个错误,说它在当前上下文中不存在。这是一个班级作业。我不确定为什么它不能识别这个函数。
using System;
public class Program
{
public int W(int a, int b)
{
return 0;
}
public static void Main()
{
int j= W(1,1);
}
}
我有一个关于关键字static的问题。比如说,我们有一段代码。
public class Foo
{
private int age;
private static int weight;
..
...
}
假设你在main中创建了2个对象。你先改变其中一个的年龄,然后再改变另一个的权重。这是否意味着在第一个对象中权重也会发生变化?如果是这样,那么这是否意味着权重是一个指针?
我想我的问题大概是。静态在内部是如何工作的?它本质上是一个指针类型吗?
我在我的MVC应用程序中有一个方法,它创建一个pdf文件(将一个具有数据写入和路径的对象作为参数)。我在一个单独的类中编写了这个方法,并使它是静态的。在我的控制器上的另一个函数中,我像这样调用这个方法:
PdfGenerator.GeneratePdfMethod("write this string", "path");
现在,如果我将此方法更改为非静态方法,则必须初始化一个PdfGenerator对象,然后调用该对象上的函数:
PdfGenerator p = new PdfGenerator();
p.GeneratePdfMethod("writ
静态对象如何访问非静态字段,即使它被定义为静态的呢?
public class pp {
static int x = 4;
int y = 8;
public static pp hj = new pp();
public static void main(String[] args) {
System.out.println(hj.y); //prints 8
}
}
我有以下类:
public abstract class TMSPageBaseClass : System.Web.UI.Page
{
protected DevExpress.Xpo.Session XpoSession;
}
public class CommonUtility : TMSPageBaseClass
{
protected static t_rolepermissions GetPermissions()
{
//here I want to access the variable XpoSession defined
我正在开发一个ASP.Net核心3.1MVC项目,在这个项目中,我希望创建一个定制的Validator,在这个项目中,提供的qty不能超过所订购的qty。我在ViewModel error CS0120中获得了一个模型中的编译错误(挂起)--非静态字段、方法或属性“ShipmentsViewModel.Pending”需要对象引用。
这是我的ViewModel
public class ShipmentsViewModel
{
public string CustomerName { get; set; }
public int OrderID { get; set; }
这是代码片段
static chck()//tracking how many times main has been called
{
static a=0;
int y=0;//this is not supposed to work
cout<<"Total time main has been called: ";
a++;
return a;
}
我们的老师告诉我们,静态函数不能改变或创建非静态变量,但它对我有效,为什么?
当我试图使用声明为类变量的map 'freq‘的内容时,在比较器函数中对向量进行排序(使用map按频率排序)时,我遇到了这个错误。有谁能指出我哪里出了问题,我该怎么做?下面是代码:
class Solution {
map<int, int> freq;
public:
static bool comp(int a, int b){
if(a!=b){
int c1 = freq[a];
int c2 = freq[b];
if(c1!=c2)
我有一个类,我应该有一个私有数组。
private int[] array = new int[5];
我应该有一个公共方法,将数据放入数组中,但我不确定如何让它正常工作。
public static void method(int a, int b)
{
`array[a-1] = b;`
}
我试过了,它给我一个错误,告诉我非静态字段、方法或属性'Class.array‘需要一个对象引用。有人能帮我弄清楚如何处理这个问题吗?
下面是我的示例代码:
public class Hybrid {
public static void main(String[] args) {
Cultivate cultivate1 = new Cultivate();
try{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myfile"));
os.writeObject(cultivate1);
os.close();
System.out.p
变量的概念被定义为:
变量由引入,即非静态数据成员或对象以外的引用的声明。变量的名称(如果有的话)表示引用或对象。
对象声明的相关概念定义为:
如果decl-说明符-seq不包含typedef说明符,则如果与名称关联的类型为函数类型(dcl.fct),而为对象声明(否则为),则该声明称为函数声明。
那么,考虑下面的代码,哪些实体被称为变量?
class A{
int a; // #1 `a` can be called a variable?
int& rf; // #2
};
void fun(int p, int& prf){ // #
有人能解释一下在this源代码中使用AbstractList.this.remove(lastRet);的方法吗?
这一行位于private class Itr implements Iterator<E>内部类的private class Itr implements Iterator<E>方法中。
以下是我的代码,我已经注释了错误所在:
using System;
using ScissorsPaperRock;
namespace ScissorsPaperRock
{
public class ThePlayerOption
{
public UNIT thePlayerChosenOption;
public static void Main()
{
Console.WriteLine("Select an option. 1 = Scissors, 2 = Paper, 3 = Ro
我试图在一个类中使用一个动态数组,但是我得到了“一个非静态成员必须相对于一个特定的对象”的错误。这是代码。 class Calendar
{
private:
static int holidayCount;
int * holidayDates = new int[10];
public:
static void addHolidayCount()
{
holidayCount++;
}
static int getHolidayCount()
我了解到,将一个类声明为友类使它能够使用声明它的类的内容或成员。我使用了以下代码:
#include <iostream>
using namespace std;
class two; // forward class declaration
class one {
private:
friend class two; // friend class declared
int a; // to be accessed later
public:
one() { a = 5; }
};
class two {
private:
int b;
下面的单例类运行良好,
public class Elvis
{
private static Elvis elvis = new Elvis();
private Elvis()
{
}
public static Elvis Instance()
{
return elvis;
}
}
但是,当我将return elvis;更改为return this.elvis时,我得到的是non-static variable this cannot be referenced from a static context。为
我很难弄清楚如何使用派生类方法访问和更改基类数据成员。下面的代码是我想要做的一个示例。
class A
{
protected:
string name;
public:
A():name("Name 1"){}
void DisplayA()
{
cout<<name<<endl;
}
};
class B : public A
{
public:
void Change()
{
我想知道非静态方法如何修改静态变量。我知道静态方法只能访问其他静态方法和静态变量。然而,另一面是真的吗?非静态方法只能访问非静态变量吗?例如:
public class SampleClass {
private static int currentCount = 0;
public SampleClass() {
currentCount++;
}
public void increaseCount() {
currentCount++;
}
}
这段代码是编译的,我想知道为什么会有静态访问权限。
我为学校的项目开发了一个网站,我有一个我不明白的错误。
我有一个抽象类和一个子类。抽象类实现了一个接口,因此我可以在子类中使用静态方法。
控制器使用子类。
主计长:
// CODE HERE
case "modifier" :
$titre = "Modifier un bookmark";
/* on vérifie que l'URL nous a bien transmis l'identifiant */
if (isset($_GET['id'])) {
$id = $_GET[&
我正在尝试访问另一个类的对象设置器,用于扫雷游戏,但我不确定它是否真的可行。
本质上,我有一个board类,它包含了设置板的所有逻辑,在我的main方法中,我希望提示用户输入一个int,然后将这个值传递给Board.setHealth()。
我在主方法中的提示符(包含在Mines.java中)如下
if (keyboard.nextInt() < 1) {
throw new Exception("Number must be higher than 0!");
}
else health = keyboard.nextInt();
Board.setHealth(h
//ClassA.h
Class ClassA
{
private:
UtilityClass utilityCls; // this is the instance that I need to access
virtual void Read();
static bool IsValid(char c);
}
//ClassA.cpp
void ClassA::Read()
{
....
string str = "abcdefg"; // sample only
if(find_if(str.begin(),
我有:
class A {
static $instances = 0;
public $instance;
public function __construct() {
$this->instance = ++self::$instances;
}
}
$a = new A();
$b = new A();
echo "<pre>";
print_r($a);
print_r($b);
echo "</pre>";
产出:
A Object
(
[instance]