overloading

overloading a simple = operator

淺唱寂寞╮ 提交于 2019-12-12 03:57:32
问题 I am trying to overload part of my class to a string and I can't get the overloading to work. Alternatively, I will also have a long long overload, but I just assume that it will be the same excepted for long long instead of string . class FileData { public: string extensions_; unsigned long long containsBytes_; }; string& operator = (string& s , FileData& fd) { s= fd.extensions_; return s; } string extName = fileVector[0]; The error I keep getting is ERROR:'operator=' must be a member

Better way to overload methods in C#

你说的曾经没有我的故事 提交于 2019-12-12 03:27:43
问题 So I have a method with some heavy overloading. However, the concept is fairly simple. "Accept any of there X data types as the first argument, then accept either of these two data types for the two remaining arguments". Is there a simpler way to do this? This is getting out of hand very fast. //Declared MyMethod(byte[], SpecializedArgumentType, SpecializedArgumentType) and a string-> SpecializedArgumentType version of it. public static MyReturnType MyMethod(bool data, SpecializedArgumentType

Possible to overload operator* to multiple an int and a char*?

送分小仙女□ 提交于 2019-12-12 02:44:08
问题 I would like to get functionality so I can do this: std::cout << "here's a message" << 5*"\n"; I tried the following: std::string operator* (int lhs, const char* rhs) { std::string r = ""; for(int i = 0; i < lhs; i++) { r += rhs; } return r; } And I got this error message: error: ‘std::string operator*(int, const char*)’ must have an argument of class or enumerated type According to the answers in this SO post What does 'must have an argument of class or enumerated type' actually mean it

Add FUNCTION, LINE, time information to all existing “couts” [duplicate]

六眼飞鱼酱① 提交于 2019-12-12 02:39:17
问题 This question already has answers here : How to override cout in C++? (9 answers) Closed 3 years ago . I already have a large code base that uses a lot of couts. I cannot go and change all the existing couts. Is there something I can do so that, the existing couts add FUNCTION , LINE and time information along with what needs to be printed in the cout? In otherwords can I override cout to use my implementationwhich will print the string along with all the extra information. I am open to doing

Overloading in Scala when type parameters are being used

我怕爱的太早我们不能终老 提交于 2019-12-12 02:14:08
问题 Look at this piece of code I am working on at the moment (It's meant to parse some arguments from the arguments of the main method): def parser[T](identifier: String, default: T, modifier: String => T): T = { val l = args.filter(_.startsWith(identifier)) if(l.isEmpty) default else modifier(l(0).drop(identifier.length).trim) } I also intended to write an overloaded method that deals with the case of Strings: def parser(identifier: String, default: String): String = parser[String](identifier,

How do I incorporate method overloading to call different parameter types for one method name in Java 8?

不问归期 提交于 2019-12-11 23:14:01
问题 I am trying to change my code to incorporate an int and double parameter for one method name. My end goal is to let the user pick two numbers and if they type one as int and the other as double, I want the code to still be able to account for those different types and print successfully. The code as follows is the basics I have come up with so far and I would like some help on how to change this code to use method overloading. import java.util.Scanner; public class SimpleCalculator { public

Overloading Class Member Function

試著忘記壹切 提交于 2019-12-11 20:25:01
问题 I have overloaded a function fn as fn(int,char) & fn(int&,char&) as shown below: #include <iostream> using namespace std; void fn(int a, char c); void fn(int& a, char& c); int main() { int a=10; char c= 'c'; cout << "Inside main()" << endl; cout << hex << "&a : " << &a << endl; cout << hex << "&c : " << (int *)&c << endl; static_cast<void(*) (int&, char&)> (fn)(a, c); return 0; } void fn(int a, char c) { int tempInt; char tempChar; cout << "\n\nInside Call By Value Function " << endl; cout <<

Overloading >> operator for Date class in c++ causes infinite loop

元气小坏坏 提交于 2019-12-11 19:48:34
问题 I'm trying to overload the >>operator for a Date class in c++ but the it goes in infinite loop when the run goes into the first if statement, can you please help me? //operator istream& operator >>(istream& is,CustomDate& d){ int day,month,year; char ch1,ch2; string test; is>>skipws>>day>>ch1>>month>>ch2>>year; if(!is){ is.clear(ios_base::failbit); return is; } if(ch1!='/' || ch2!='/') error("invalid date input"); d = CustomDate(day,month,year); return is; } This is the function that calls it

overloading operators, what is the purpose of the overladed operator in this example

南楼画角 提交于 2019-12-11 18:40:06
问题 I have been trying to understand the use of the overloaded operator in this code, however, I cannot wrap my mind around it. I do not understand exactly the purpose of the overloaded operator or why it is needed. I know that this is a newbie question but if someone could explain it I would greatly appreciate it. const int Card::operator+(const Card& B) { if ((faceValue ==1 || B.faceValue == 1) && (faceValue + B.faceValue)<= 11) return 10 + faceValue + (B.faceValue); else return faceValue + (B

program not calling the right overloaded operator

不羁的心 提交于 2019-12-11 18:22:23
问题 I've the class Directory which does stuff with objects of class File, and overloaded operators, like this: class Directory { std::vector<char> name, timeOfCreation; std::vector<File> arr; public: Directory(std::string); Directory(const Directory&); ~Directory(); Directory operator += (File); Directory operator += (Directory); Directory operator -= (File); Directory operator ~(); File operator [] (int); operator int(); friend bool operator % (Directory, File); friend bool operator % (Directory