inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Sept 12, 2011 17:19:49 GMT -8
#include <iostream> #include <string> using namespace std;
class MyString{ public: void subString(){ A = "How Are You"; B = "Are "; S = ""; S = A-B; }
string A; string B; string S; };
string operator-(MyString src){ MyString(); string X = src.A; string Y = src.B; string Z = src.S; int N = X.find(Y); Z = X.replace(N,Y.length(),""); return Z; }
void main(){ MyString run; run.subString(); }
Does anyone know why when it trys to do A-B I get a warning that says:
no operator "-" matches these operands
I tried to overload it thanks.
|
|
inherit
123128
0
Feb 3, 2020 13:53:38 GMT -8
Malagrond
Remember, remember the 5th of November.
813
April 2008
malagrond
|
Post by Malagrond on Sept 13, 2011 6:51:16 GMT -8
I'm a bit rusty, but try putting a space after "operator-"? It might be trying to interpret it as an arithmetical operation.
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Sept 13, 2011 8:07:34 GMT -8
I'm a bit rusty, but try putting a space after "operator-"? It might be trying to interpret it as an arithmetical operation. It doesn't matter if there are spaces or not because C++ knows you are trying overload the operator. You could add spaces all around it or have none. There are several problems, though. First, the overloaded function you are trying to define is being declared after your class' definition so the compiler doesn't even know about it yet. Second, the function's prototype hasn't been added to the string class' definition so the compiler definitely doesn't know about the function. Third, you need to say which class you are overloading, which means you'd need to add the red: string string::operator-(MyString src){ . But now, the string class won't know what the "MyString" class is, so you'd have to add "class MyString;" at the top of the string class' header file. You'd also have to add the function itself to the string.h file which I don't recommend because compiling on another computer wouldn't work. Just create another function that doesn't overload anything. Add parameters for the strings (which I recommend passing by reference so you don't create a copy of the entire string). Let me know if none of this makes sense.
|
|
inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Sept 13, 2011 16:26:06 GMT -8
Thanks for the help guys I figured out my problem!
|
|