几天前我开始用C#编程.
现在,当玩弄运算符重载时会出现一个令人困惑的错误.
以下代码在运行时生成StackOverflowException:
using System;
namespace OperatorOverloading
{
public class Operators
{
// Properties
public string text
{
get
{
return text;
}
set
{
if(value != null)
text = value;
else
text = "";
}
}
// Constructors
public Operators() : this("")
{
}
public Operators(string text)
{
// Use "set" property.
this.text = text;
}
// Methods
public override string ToString()
{
return text;
}
// Operator Overloading
public static string operator +(Operators lhs, Operators rhs) …Run Code Online (Sandbox Code Playgroud) 首先,我使用模板创建了一个双向链表.我有一个"帐户"类,我已经重载了"=="运算符来比较帐户ID.我创建了一个链接列表来保存帐户.
当我向列表中添加一个新帐户时,它会调用"包含?" 调用==运算符的方法.这是调用错误的地方,g ++告诉我
sll.h:在成员函数'bool list :: contains(T)[with T = account]'中:
customer.h:25:35:从这里实例化
sll.h:261:3:错误:'temp-> node :: data == item'中'operator =='不匹配
account.h:36:6:注意:候选人是:bool account :: operator ==(account&)
我现在一直在调查几个小时,我无法深究它.我怀疑它可能与我使用模板这一事实有关.我创建了测试程序,看看我是否正确地重载了操作符并且它按预期工作.
另请注意:有一个客户类,它包含在帐户列表中,这就是所谓的"添加"方法.不幸的是,我不能发布超过2个超链接,所以我只能说出这个课程是正确的.= P
由于代码有点长,我使用了pastie:
我在c ++中练习单链表(练习如何找到循环列表的起始节点),但发现使用运算符 - >非常混乱.我正在使用Visual Studio 2010 C++ Express
这非常有效: head->append(2)->append(3)->append(4)->append(5)
但这不起作用(创建循环链表): head->append(2)->append(3)->append(4)->append(5)->append(head->next)
当我跳转到这个方法并进行调试时,似乎head->next没有正确地传递给方法.
但这有效:
Node* tail=head->append(2)->append(3)->append(4)->append(5);
tail->append(head->next);return c->next到return head在这两种方法,head->append(2)->append(3)->append(4)->append(5)->append(head->next) 也适用.我在这里错过了什么?谢谢!
我的代码详情如下:
void main(){
Node* head=new Node(1);
Node* tail=head->append(2)->append(3)->append(4)->append(5)->append(head->next);
cin.get();
}
class Node{
public:
Node* next;
int data;
bool marked;
Node(int d){
data=d;
marked=false;
next=NULL;
}
Node* append(int d){
Node* c=this;
while(c->next!=NULL){
c=c->next;
}
c->next=new Node(d);
return c->next;
}
Node* append(Node* n){
Node* c=this;
while(c->next!=NULL){
c=c->next;
}
c->next=n;
return c->next; …Run Code Online (Sandbox Code Playgroud) 我对F#中的默认" = "(等于)运算符有疑问.它允许比较用户定义的联合类型.问题是:它的复杂性是什么?例如,让我们考虑以下类型:
type Tree<'a> =
| Nil
| Leaf of 'a
| Node of Tree<'a> * Tree<'a>
Run Code Online (Sandbox Code Playgroud)
以及树木:
let a : Tree<int> = Node (Node (Node (Leaf 1, Leaf 2), Node (Leaf 3, Node (Leaf 4, Leaf 5))), Node (Leaf 6, Nil))
let b : Tree<int> = Node (Node (Node (Leaf 1, Leaf 2), Node (Leaf 3, Node (Leaf 4, Leaf 5))), Node (Leaf 6, Nil))
let c : Tree<int> = Node (Node (Node (Leaf 1, Leaf 2), Nil), …Run Code Online (Sandbox Code Playgroud) complexity-theory f# equals user-defined-types operator-keyword
我想打印出一个用户定义类型的对象,就像cout << ob1;
这样我想重载operator <<并且我希望按值返回而不是通过引用但是它给了我一个错误:在两个名为iosfwd和ios_base的文件中. H
ostream operator<<( ostream& out, cat& rhs){
out << rhs.a << ", " << rhs.b << endl;
return out ;
}
Run Code Online (Sandbox Code Playgroud)
1)是因为它不能创建一个新的ostream对象,这就是为什么它必须通过引用返回?
但是当我像这样通过引用返回时:
ostream& operator<<( ostream& out, cat& rhs){
out << rhs.a << ", " << rhs.b << endl;
return out ;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.
2)有什么解释吗?
我最近看过GoingNative2012当然是关于C++ 11.
在Bjarne Stroustrup部分,我发现有一个有趣的运算符函数,如下所示.
constexpr Value<Second> operator""s(long double d)
{
return Value<Second>(d);
}
Run Code Online (Sandbox Code Playgroud)
好吧,除了在C++ 11中看起来像新关键字的constexpr之外,
我从来不知道""是否可以超载?
这是C++中的新功能之一,虽然我没能使用VS 2010测试它吗?
提前致谢.
我正在尝试编写一个小的内存泄漏检测工具。
我的想法是跟踪应用程序中的动态内存分配寿命,以确定可能导致我的应用程序在使用过程中成为核心的任何无效的内存访问或未删除的内存。
我想编写一个简单的接口来覆盖new和delete。
在我的覆盖下,new我想打印功能行地址等,然后调用标准new。
有没有人尝试过?我不确定我是否可以从班级特定的新运算符中调用标准新。
我知道数字字符串可能是PHP中的类型,但是我不知道为什么会发生这种情况或给出这个结果:
$a="00010010001101000000101";
$b="00010010001101000000001";
$c = (($a == $b) ? "true" : "false");
$d = (($a === $b) ? "true" : "false");
echo $c . " " . $d . "\n"; // true false
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,$ a和$ b的定义方式相同,长度相同,但内容不同,很多字符.($ a == $ b)如何评估为真?
Websocket片段有一个声明,在这样的封闭的parens中有美元符号,
any ($ fst client)
Run Code Online (Sandbox Code Playgroud)
因为haskellers使用$sign而不是parens,为什么我们需要parens?
为什么$parens之间有符号?
我试着看看是否$是一个功能
Prelude>:t $
Run Code Online (Sandbox Code Playgroud)
但它抛出了错误, parse error on input $
在Python中,我们有一个很好的简单语法来将任意int/float赋予幂.也就是说,对于非Python程序员,我们可以有以下声明:
y = 2 ** 3
print y
Run Code Online (Sandbox Code Playgroud)
这将打印8到控制台,并具有良好的语法,因为有一个内置的"电源"操作符.是否可以在C++中将"**"重载为单个运算符?具体来说,我想完成这样的事情:
int a = 2;
int b = 3;
cout << (a**b) << endl;
Run Code Online (Sandbox Code Playgroud)
或者,如果这是不可能的,这样的事情:
MyInt a = new MyInt(2); // Wrapper class around ints to play nicely with **
MyInt b = new MyInt(3);
cout << (a**b) << end; // Assume ostream overridden for MyInt
Run Code Online (Sandbox Code Playgroud)
这些也应该打印8到控制台.我理解覆盖"^"运算符来做同样的事情要容易得多,但我最感兴趣的是看是否可以重载"**".运算符"*"(对于MyInt类的情况,如果它是成员函数)是否必须查看参数是否是另一个"*",因为我不知道如何指定"**"作为一个运营商?甚至可以将运算符作为参数传递吗?
如果可能的额外/奖金规定(好像我还没有说够):没有宏!!!
operator-keyword ×10
c++ ×6
overloading ×3
c# ×1
c++11 ×1
dollar-sign ×1
equals ×1
f# ×1
haskell ×1
memory ×1
memory-leaks ×1
new-operator ×1
operators ×1
php ×1
syntax ×1
templates ×1