还有一个问题,转到我!...无论如何,我有两个私有构造函数和静态函数的类来返回该类的实例.一切都很好,我有一个main.cpp文件,我设法得到我的gameState对象指针,通过这样做:
gameState *state = gameState::Instance();
Run Code Online (Sandbox Code Playgroud)
但现在我似乎遇到了问题.为方便起见,我希望gameState实例和actionHandler实例都保留指向对方的副本.所以我试图在彼此的头文件中包含:
gameState *state;
Run Code Online (Sandbox Code Playgroud)
和
actionHandler *handler;
Run Code Online (Sandbox Code Playgroud)
但是,这似乎不起作用......我得到"错误C2143:语法错误:缺少';' 在这两行之前的'*'"错误之前......如果该类有私有构造函数,你能否在头文件中定义某个classe的变量?或者是其他问题?或许是因为指向teh实例的指针存储为静态成员?
编辑:谢谢你们!令人惊讶的是我最近几天获得的c ++知识量很多......真棒!
我在这里有一个简单的问题。如果我在主类中 [declared] 的对象中声明一个变量,如下所示:
public static int number;
Run Code Online (Sandbox Code Playgroud)
(通常我这样声明:
private int number;
Run Code Online (Sandbox Code Playgroud)
)
它可以在主类中[声明]的不同对象中使用吗?顺便说一句,我不在乎安全性 atm,我只是想让一些东西工作,不在乎保护)
请帮忙,
问题:以下代码中的核心转储:
我有一个抽象类 SomeOtherClass,并从它派生了 SomeOtherClassImpl。
这是导致问题的代码:
class MyClass
{
public:
void someFunction()
{
myVector().push_back(someOtherClassDefault());
}
private:
static std::vector<SomeOtherClass const *> & myVector()
{
static std::vector<SomeOtherClass const *> theVector;
return theVector;
}
static SomeOtherClass const * someOtherClassDefault()
{
static SomeOtherClassImpl theDefault;
return &theDefault;
}
};
Run Code Online (Sandbox Code Playgroud)
我在其他翻译单元中有一些 MyClass 类型的静态变量。
这个问题很奇怪,因为程序退出时会发生分段错误。当然 theDefault 可以在 theVector 之前被释放,但是有什么区别呢?当 main 已经完成时,两者都被解除分配。
您的帮助将不胜感激。
对不起我令人毛骨悚然的英语。在 Qt 4.8.4 中,我尝试使用静态 QString 字段创建单例类。它不能是 const,因为这个字段会在运行时改变值。通过常量 QString 初始化此字段,在同一文件中声明。
不幸的是,这是行不通的,并且在初始化字符串中
LogWay = QString("file.txt");
Run Code Online (Sandbox Code Playgroud)
程序意外以错误结束
下级停止了,因为它收到了来自操作系统的信号。信号名称:SIGSEGV 信号含义:分段错误
在文件“qatomic_i386.h”中
我做错了什么?此代码示例适用于 int、bool 或 double 变量,但不适用于 QString。为什么?我尝试在没有 QString 构造函数的情况下编写,使用简单的等式
LogWay = "...";
Run Code Online (Sandbox Code Playgroud)
但我有同样的错误。感谢您的帮助。
类的完整代码:
这是 .h 文件 #include
const double _ACCURACY_ = 0.0001;
static const QString _LOG_WAY_ = "GrafPrinterLog.txt";
class GlobalSet
{
private:
static QSettings *_Settings;
GlobalSet(){}
GlobalSet(const GlobalSet&){}
GlobalSet &operator=(const GlobalSet&);
static GlobalSet *GS;
public:
static double IntToCut;
static double ReplaceSize;
static double Accuracy;
static int MaxPointCount;
static bool NeedProper;
static QString LogWay; …Run Code Online (Sandbox Code Playgroud) 我有以下课程
import java.util.ArrayList;
import java.util.List;
public class TestStaticArrayList {
public static List<String> numberList = new ArrayList<String>();
public static List<String> getArrayValues(){
return numberList;
}
public static void populateArray() {
numberList.add("1");
numberList.add("2");
numberList.add("3");
}
}
Run Code Online (Sandbox Code Playgroud)
上面类中的静态ArrayList是从下面的类调用动态填充的.
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class TestStaticInvokationClass {
public static void main(String[] args) throws Exception {
Class staticKlass = Class
.forName("com.sathish.test.TestStaticArrayList");
staticKlass.getMethod("populateArray", null).invoke(null, null);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图访问下面的类中的静态ArrayList.但它总是为空.
public class StaticArrayListAccessTest {
public static void main(String[] args) throws Exception {
System.out.println(TestStaticArrayList.getArrayValues());
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用
static …Run Code Online (Sandbox Code Playgroud) 在SO上有一些类似的问题,但它们并不是一样的安静,所以我发布了这个问题.我是MVVM的新手,所以我试图找出如何创建一个可以保存可以在视图之间共享的属性的类.因此,如果我在一个视图中设置了一个属性,那么所有其他视图会在其更改后得到通知,并相应地调整其属性.
我现在拥有的是相当粗糙的,绝对不是我想要使用的东西.这是我的常用类,它将包含所有属性:
public static class Common
{
private static string _title;
public static string Title
{
get { return _title; }
set
{
if (_title == value)
{
return;
}
_title = value;
OnPropertyChanged("Title");
}
}
public static void Load()
{
// set properties here
}
public static event PropertyChangedEventHandler PropertyChanged;
private static void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(typeof(SettingsWorker), new PropertyChangedEventArgs(name));
}
}
}
Run Code Online (Sandbox Code Playgroud)
...我必须从每个ViewModel订阅它:
Common.PropertyChanged += Common_PropertyChanged;
private void Common_PropertyChanged(object sender, …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么我收到这个错误.任何人都可以伸出援助之手.我需要在头文件中声明VideoCapture捕获并在Video.cpp中调用它
Video.h
class Video
{
public:
static VideoCapture capture;
//Default constructor
Video();
//Declare a virtual destructor:
virtual ~Video();
//Method
void Start();
private:
};
Run Code Online (Sandbox Code Playgroud)
Video.cpp
#include "StdAfx.h"
#include "Video.h"
#include "UserInfo.h"
#include "Common.h"
void Video::Start()
{
while(1)
{
Mat img;
bool bSuccess = capture.read(img); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "End of video" << endl;
break;
}
imshow("original video", img); //show the frame in "Original Video" window
if(waitKey(30) == 27) …Run Code Online (Sandbox Code Playgroud) 静态变量存储在非静态方法调用中的哪里?即内部CalculateTotalStatic(),我们正在递增静态成员MyStaticVariableHolder.Total,并且也比较变量i与MyStaticVariableHolder.TotalArray.Length内部的for循环.
另一方面,在此方法的另一个版本中CalculateTotalLocal(),我们使用在方法中声明的局部变量来执行上述操作.
在此期间CalculateTotalLocal,堆栈上将放置两个附加变量,这些变量将在堆栈本身(localTotal和localLength)上保存它们的值.在这种情况下会发生什么CalculateTotalStatic?它每次都从堆中访问静态变量吗?而且,CalculateTotalLocal比10%快CalculateTotalStatic.这种性能提升的正确原因是什么?
编辑 - 我想我不是很清楚 - 为此道歉.
我想说的是:
可以(根据C#规范)通过兼容的C#编译器/ JIT以与局部变量相同的方式优化静态变量访问?
class MyStaticVariableHolder
{
public static int[] TotalArray = new int[100];
public static int Total = 1;
}
class Trial
{
public void CalculateTotalStatic()
{
for (int i = 0; i < MyStaticVariableHolder.TotalArray.Length; i++)
{
MyStaticVariableHolder.Total += i;
}
}
public void CalculateTotalLocal()
{
int localTotal = MyStaticVariableHolder.Total; …Run Code Online (Sandbox Code Playgroud) 我有一个问题,从C#到F#重写Akka.Net演员:
public class Listener : ReceiveActor
{
public Listener()
{
Receive<Messages.Shutdown>(s =>
{
Console.WriteLine($"shutdown {s.Duration}");
Context.System.Terminate();
});
}
}
Run Code Online (Sandbox Code Playgroud)
Actor应该通过终止actor系统来处理Shutdown消息.我试图像这样重新实现它:
type Listener() =
inherit ReceiveActor()
do
base.Receive<Messages>(fun m ->
match m with
| Shutdown(duration) ->
printf "shutdown %s" (duration.ToString())
base.Context.System.Terminate()
true
| _ -> false)
Run Code Online (Sandbox Code Playgroud)
但是有一个补充错误在线base.Context.System.Terminate()说Property 'Context' is static.这段代码有什么问题?为什么我不能访问基类的静态属性?是因为这段代码是lambda expresion(有趣)吗?或者因为它在构造函数中(do)?
当我编译包含以下头文件的代码时,出现错误消息:
Graph.h:22: error: ISO C++ forbids in-class initialization of non-const
static member `maxNumberOfNeighbors'
Run Code Online (Sandbox Code Playgroud)
如何声明和初始化不是const的静态成员?
这是.h文件
#ifndef GRAPH_H
#define GRAPH_H
typedef char ElementType;
class Graph {
public:
class Node {
public:
static int maxNumberOfNeighbors = 4;;
int numberOfNeighbors;
Node * neighbors;
ElementType data;
Node();
Node(ElementType data);
void addNeighbor(Node node);
};
typedef Node* NodePtr;
Graph();
void addNode(Node node);
NodePtr getNeighbors(Node node);
bool hasCycle(Node parent);
private:
NodePtr nodes;
static int maxNumberOfNodes;
int numberOfNodes;
};
#endif /* GRAPH_H */
Run Code Online (Sandbox Code Playgroud)