我正在尝试使用默认值创建构造函数.复杂性来自于为类使用单独的头文件和代码文件.我有一个包含以下内容的头文件:
class foo {
bool dbg;
public:
foo(bool debug = false);
}
Run Code Online (Sandbox Code Playgroud)
一个代码文件包含:
foo::foo(bool debug = false) {
dbg = debug;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试用g ++(即g++ -c foo.cc)编译时,它会给出一个错误:
foo.cc:373:65: error: default argument given for parameter 1 of ‘foo::foo(bool)’
foo.h:66:4: error: after previous specification in ‘foo::foo(bool)’
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有以下设置:
public abstract class A
{
public void f()
{
//Want to make an instance of B or C here
//A bOrC = new ?
}
public abstract void f2();
}
public class B : A { public override void f2(){} }
public class C : A { public override void f2(){} }
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样的话?
编辑: bOrC需要是从中f()调用的特定派生类的类型
如何在Assert.AreEqual(object, object)(命名空间中的方法Microsoft.VisualStudio.TestTools.UnitTesting)确定参数是否相同?它是否使用该Object.Equals(object, object)方法(在System命名空间中)?
我正在为WCF Web服务编写单元测试.我故意向服务器发送无效请求,抛出一个WebExceptionFault<string>.在单元测试中,被抓住的异常是EndpointNotFoundException与标准WebException的InnerException属性.我想验证响应的主体是否匹配我认为应该存在的字符串.
然而,我遇到了一个问题,因为(这是一个)的Response属性是Disposed并且无法读取.WebExceptionSystem.Net.SyncMemoryStream
我的代码:
Clubs actual;
try
{
//"201" is an invalid argument for the first parameter
actual = new Clubs(channel.GetClubs("201", "123"));
}
catch (EndpointNotFoundException e)
{
WebException w = e.InnerException as WebException;
Assert.IsNotNull(w);
HttpWebResponse resp = w.Response as HttpWebResponse;
Assert.IsNotNull(resp);
Assert.AreEqual(resp.StatusCode, HttpStatusCode.NotFound);
//Next line throws an ArgumentException saying Stream not readable
//Investigation shows it has been disposed
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
Assert.AreEqual(sr.ReadToEnd(), "League not allowed …Run Code Online (Sandbox Code Playgroud) 请考虑以下示例:
struct ConvertibleStruct {};
enum class ConvertibleEC {};
struct Target {
// Implicit conversion constructors
Target(ConvertibleStruct) {}
Target(ConvertibleEC) {}
};
Target operator~(const Target& t) {
return t;
}
Target anotherFunction(const Target& t) {
return t;
}
int main() {
ConvertibleStruct t;
ConvertibleEC ec;
~t; // 1. Works finding the operator overloaded above
~ec; // 2. Fails to compile on clang 3.4 and gcc 4.8.2
operator~(ec); // 3. Works finding the operator overloaded above
anotherFunction(ec); // 4. Works
}
Run Code Online (Sandbox Code Playgroud)
编译器版本:
以上调查结果适用于 …
c++ operator-overloading implicit-conversion c++11 enum-class
c# ×3
c++ ×2
.net ×1
assert ×1
c++11 ×1
class ×1
constructor ×1
enum-class ×1
exception ×1
mstest ×1
polymorphism ×1
unit-testing ×1