如果我有这样的类,我该如何编写复制构造函数?
#include <stringstream>
class MyClass {
std::stringstream strm;
public:
MyClass(const MyClass& other){
//...
}
std::string toString() const { return strm.str(); }
};
Run Code Online (Sandbox Code Playgroud)
std :: stringstream本身没有复制构造函数,所以我不能使用这样的初始化列表:
MyClass(const MyClass& other): strm(other.strm) {}
Run Code Online (Sandbox Code Playgroud) 我在项目中成功使用了gcov:
-fprofile-arcs -ftest-coverage-lgcov选项链接gcda并且gcno产生了许多和文件。gcov产生了很多文件。#####: 42: virtual double run_time() const { return 0; }然后我走了!并编写一个调用该缺失方法的测试。
上面的示例行很容易诊断-gcov告诉我我没有调用的确切方法。
我也有一个示例,该示例由gcov标记为复制构造函数,但是我可以使用Visual Studio调试器逐步完成它。那里的解决方案是意识到gcov遭受RVO的困扰,从而使副本无法使用,但是编写了一个测试来强制副本修复该问题。
我还有其他一些我无法弄清楚的例子:
1.
File.cpp
#####: 78:}
Run Code Online (Sandbox Code Playgroud)
gcov似乎在标记名称空间的右括号,该名称空间是文件的最后一行。
2.
File.h
#####: 33: class FooBase: public IResult {
Run Code Online (Sandbox Code Playgroud)
gcov想在这里告诉我什么?我想不起在这里打电话。
更新1:我发现FooBase有一个默认的构造函数,就子类而言,如果仅由子类“调用”,则与通过实例化它的调用不同gcov。
更新2:我一直在使用djgpp/gcc 4.4.4它产生以上结果。但是,通过使用MinGW/gcc 4.5.2“像差”消失以及更多的工作,我已经可以达到100%的线路覆盖率。
请为不熟练的gcov用户写一个提示,或者给出我的示例之一的答案。
我想更新矢量'v',以便我可以从0-100开始迭代.
我知道这是不允许的,但如果我只想这样做怎么办?有什么办法吗?
int main() {
// your code goes here
vector<int> v;
v.push_back(1);
int count = 0;
for(int elem: v){
if(count<100)
v.push_back(count);
count++;
}
for(int elem: v)
cout << elem << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
1
0
Run Code Online (Sandbox Code Playgroud) 我看到不同类别的不同图标,如下图所示:

这是什么意思?其中一些带有红色图标的我可以添加注释,但是例如带有"c"和"ray"的那些不允许我.
这些评论提到了类责任合作者(CRC)设计,但不清楚评论如何影响图标.
有人可以向我解释为什么我的重载++(预版本)没有更新值吗?片段是这样的:
circle circle:: operator++()
{
Area = Area * 2.0;
return *this;
}
/////////////////////////////
int main()
{
class circle c1(4, 1, -1), c2(12, 4, 6);
c1.output();
c1++;
c1.output();
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) I was told that if an obj's __str__ method isn't created but __repr__ is created then printing the obj will invoke __repr__. That appears to be true for normal class, but when I try it on Exception's subclass, it is weird that __repr__ doesn't get invoked, could anyone explain why?
Here's an example
class BoringError(Exception):
def __init__(self):
self.purpose = "Demonstration"
self.boringLevel = 10
def __repr__(self):
return "I'm a message for developer"
try:
if 1 != 2:
raise BoringError
except …Run Code Online (Sandbox Code Playgroud) 我有一些这样的对象:
Obj o1{};
Obj o2{};
Obj o3{};
...
Run Code Online (Sandbox Code Playgroud)
但是,我还有另一个班级Foo,其中有一个std::vector<Obj*>作为成员。
如何vector从任意列表初始化?
我想这样构造Foo:
Foo f{o1, o3, o4};
Run Code Online (Sandbox Code Playgroud)
我已经知道我没有参考资料的集合,因此尝试了此操作。
class Foo {
public:
explicit Foo(std::initializer_list<Obj&> px) {
for (auto& p : px) {
fwds.push_back(&p);
}
}
...
private:
std::vector<Obj*> fwds;
};
Run Code Online (Sandbox Code Playgroud)
我在这种情况下得到的错误是:
... include\initializer_list(25,1): error C2528: 'abstract declarator': pointer to reference is illegal
Run Code Online (Sandbox Code Playgroud) 我正在遍历我的文本文件,但是当我使用 read() 函数时,循环会遍历字母而不是句子。
使用以下代码:
for question in questions: # voor elke question moet er door alle lines geiterate worden
print(f"Question: {question}")
f = open("glad.txt", "r")
text = f.read()
# text = text.replace("\n", ". ")
# text = text.replace(". .", "")
# text = text.replace(".. ", ". ")
# text = text.replace(".", ".\n")
#text = text.strip(".. ")
# test = text.replace('[bewerken | brontekst bewerken]', "")
# output = re.sub(r'\[\d+\]', '', test)
for line in text:
text = str(line) #het antwoord moet …Run Code Online (Sandbox Code Playgroud) 我有一个内存泄漏的问题.我有一个基类指针.从中,我new用来分配不同的派生类.然后,当我尝试delete使用引用(不是类型转换)的那些类时,我得到内存泄漏.我研究了这个问题,发现我应该在基类中添加一个虚拟析构函数,但是我尝试了这个并且仍然有内存泄漏; 也就是说,根据我的任务管理器,每次使用基类指针分配和删除派生类时,内存使用量会继续增加.我试图使它成为一个抽象的析构函数,并在派生类中添加了析构函数,但是我得到了一个未定义的引用错误.我也尝试将指针类型转换为派生类指针delete,但显然这会使程序崩溃.
有谁知道我应该怎么做?
示例代码:
class A {
public:
A();
~A() {};
virtual ~A(); /*or*/
virtual ~A()=0; /*or*/
/*or nothing?*/
}
class B: private A {
public:
B();
~B() {}; /*this?*/
/*or nothing?*/
}
Run Code Online (Sandbox Code Playgroud) 我正在使用http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/#comment-103785开发GCM示例应用程序.
我无法注册gcm服务器.我收到以下错误.我已经搞砸了.但我找不到解决方案,所有的解决方案都是关于jar文件我已经做了所有的更改,但我无法得到它.
错误
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.gcm.GoogleCloudMessaging
com.sanjeev.integrationapp.RegisterActivity.registerGCM(RegisterActivity.java:80)
com.sanjeev.integrationapp.RegisterActivity$1.onClick(RegisterActivity.java:47)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
我的课程代码如下
public class RegisterActivity extends Activity {
Button btnGCMRegister;
Button btnAppShare;
GoogleCloudMessaging gcm;
Context context;
String regId;
public static final String REG_ID = "regId";
private static final String APP_VERSION = "appVersion";
static final String TAG = "Register Activity";
@Override
public void onCreate(Bundle savedInstanceState) { …Run Code Online (Sandbox Code Playgroud) c++ ×6
python ×2
android ×1
c++11 ×1
for-loop ×1
gcc ×1
gcov ×1
memory-leaks ×1
pharo ×1
polymorphism ×1
python-3.x ×1
smalltalk ×1
stl ×1
text ×1
vector ×1