我有一个Parser.h,Parser.cpp文件,其中包含构造函数
Parser::Parser(string filename){....}
Run Code Online (Sandbox Code Playgroud)
现在我想在我的functions.cpp文件中使用我的解析器,我确保在那里
#include "Parser.h"
Run Code Online (Sandbox Code Playgroud)
在我的函数中,我想使用解析器如何创建它的新实例,这是我所拥有的,但我得到语法错误.
Parser::Parser parser("filename.txt");
Run Code Online (Sandbox Code Playgroud)
对不起,我应该确保我正确复制了我的代码,我没有引用错误,也没有忘记包括parser.h
这是我为我发现的问题创建的一个测试用例.
由于某种原因,B()中的dict()'l'似乎没有保持正确的值.请参阅我的Linux 11.04 Ubuntu上的输出,python 2.7.1+.
class A():
name = None
b = None
def __init__(self, name, bname, cname, dname):
self.name = name
print "A: name", name
self.b = B(name, bname, cname, dname)
print "A self.b:", self.b
class B():
name = None
l = dict()
c = None
def __init__(self, name, bname, cname, dname):
self.aname = name
self.name = bname
print " B: name", bname
self.c = C(bname, cname, dname)
self.l["bb"] = self.c
print " B self:", self
print " B …Run Code Online (Sandbox Code Playgroud) 可能重复:
带空括号的默认构造函数
实例化带或不带括号的类?
程序:
class Foo
{
public:
Foo ( int bar = 1 )
{
cout << "bar=" << bar;
}
};
int main() {
cout << "0 - ";
Foo foo_0 ( 0 ) ;
cout << '\n';
cout << "1 - ";
Foo foo_1 ();
cout << '\n';
cout << "2 - ";
Foo foo_4;
cout << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
0 - bar=0
1 -
2 - bar=1
Run Code Online (Sandbox Code Playgroud)
问题:为什么示例#1不起作用,而示例#0和#2呢?
struct xampl {
int x;
char y;
};
struct xampl InstaceOfxampl;
struct xampl *PointerToxampl;
struct xampl &new_struct;
Run Code Online (Sandbox Code Playgroud)
我们在第一种情况下创建一个简单的实例,在第二种情况下创建一个指向xampl结构的指针.但是第三个声明是什么意思呢?与代码中的其他两个相比,它的处理方式有何不同?
我遇到了这段代码,从给定的链表中删除了偶数长度的字符串我不明白为什么迭代器对象itr没有用new关键字实例化.这是代码..
public static void removeEvenLength(List<String> list) {
Iterator<String> itr= list.iterator();
while (itr.hasNext()) {
String element=itr.next();
if (element.length()%2==0) {
i.remove();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着迭代器方法是static,它只返回一个新iterable object的listas作为它field.有人可以提供一个或多个例子,其中我在Java中遇到类似的实例化方法而不是单例构造函数.谢谢
我希望能够访问从抽象类的子类实例化的对象.这是一个例子.
// A.java
public abstract class A {
public int getNewNo() {
int newNo = <Instance of B in this example here>.getNo() + 2;
return newNo;
}
}
// B.java
public class B extends A {
public int getNo() {
return 2;
}
}
// C.java
public class C {
public C(A a) {
System.out.println("The number is "+a.getNewNo());
}
}
// example.java
public void main(String args[]) {
B b = new B();
C c = new C(b);
// should now …Run Code Online (Sandbox Code Playgroud) 我将首先解释我的问题,然后提供很好的代码部分.我用一个名为process的自定义类填充队列.使用此行:
ProcessQueue.push(new Process(inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]));
Run Code Online (Sandbox Code Playgroud)
这一切看起来都很好,当循环完成时,进程队列中充满了指针.但经过进一步检查,我发现他们都指向SAME对象?
发现这个好奇,我在循环的每次迭代中都进入了构造函数.
第一次迭代:当输入构造函数时,所有实例变量都为null(如预期的那样)
第二次迭代:在进入构造函数时,所有实例变量都包含在第一次迭代中赋予对象的值(即:相同的对象)
此后,当我使用队列时,我已经确认队列中的每个指针都指向同一个进程对象.(我可以告诉这个,因为进程包含一个状态,如果循环通过队列改变状态,我会发现状态已经改变为第二个指针)
我怀疑我一定是在创造课堂上做错了.所以这里完全是这样.
Process.h
#pragma once
class Process
{
public:
Process(int _processId, int _arrivalTime, int _CPUTime,
int _IOFrequency, int _IODuration);
~Process();
bool HasArrived(int time);
bool HasCompleted();
bool HasFinishedBurst();
bool HasFinishedIO();
int GetQueueNum();
int GetID();
void SetQueueNum(int i);
void SetToReady();
void Run();
void PerformIO();
};
Run Code Online (Sandbox Code Playgroud)
Process.cpp
#include "stdafx.h"
#include "Process.h"
#include <string>
using namespace std;
int processId;
int arrivalTime;
int CPUTime;
int IOFrequency;
int IODuration;
int Ticks;
int CPUConsumption;
int CPUBurstSize; …Run Code Online (Sandbox Code Playgroud) 我遇到了一段有趣的Java代码片段.我研究了newInstance()是什么,它意味着避免调用构造函数并创建对象的新实例.但是看一下示例代码我不明白一件事:为什么不需要构造函数?
public class SimpleContentFragment extends WebViewFragment {
protected static SimpleContentFragment newInstance(String file) {
SimpleContentFragment f=new SimpleContentFragment();
Bundle args=new Bundle();
args.putString(KEY_FILE, file);
f.setArguments(args);
return(f);
}
}
Run Code Online (Sandbox Code Playgroud)
否则此代码中的其他位置是否创建了构造函数.没有
public SimpleContentFragment() {
// Required empty public constructor
}
Run Code Online (Sandbox Code Playgroud)
正如我所料.
那么你能用newInstance澄清静态方法中发生了什么吗?new SimpleContentFragment()当构造函数从未写入时它怎么能调用?
我在Swift的Messaging App上观看了这个视频并逐字复制.然而,最近有一些Xcode的更新,所以我不确定这是不是问题,或者我输错了.我也看了类似我的其他问题,仍然找不到问题.
我放在viewDidLoad()中的代码处理显示键盘:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown", name: UIKeyboardDidShowNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
然后我在这里调用keyboardWasShow函数:
func keyboardWasShown(notification: NSNotification) {
let dict : NSDictionary = notification.userInfo!
let s : NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let rect : CGRect = s.CGRectValue()
UIView.animateWithDuration(0.3, delay: 0, options: .CurveLinear, animations: {
self.resultsScrollView.frame.origin.y = self.scrollViewOriginalY - rect.height
self.frameMessageView.frame.origin.y = self.frameMessageOriginalY - rect.height
var bottomOffset : CGPoint = CGPointMake(0, self.resultsScrollView.contentSize.height - self.resultsScrollView.bounds.size.height)
self.resultsScrollView.setContentOffset(bottomOffset, animated: false)
}, completion: {
(finished: Bool) in
//
})
}
Run Code Online (Sandbox Code Playgroud)
之所以我认为它是keyboardWasShown函数,因为输出显示了KeyboardWasShown]:无法识别的选择器发送到实例0x7f848c12be40
任何帮助,将不胜感激!
我需要调用类的实例ControlledTank来访问GetPlayer我稍后将完成的方法.但是,当我尝试创建一个实例时,它使用的是构造函数而不是类.
public class ControlledTank
{
private float Tangle;
public ControlledTank(Player player, int tankX, int tankY, Game game)
{
Tangle = 0;
//throw new NotImplementedException();
}
public Player GetPlayer()
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
我没有要解析构造函数的值,所以我只是得到错误.
