methods

C++ generic class error, what's the problem?

丶灬走出姿态 提交于 2020-06-29 06:43:11
问题 Why the following code doesn't compile? namespace mtm { template<class T> class Matrix { private: public: class AccessIllegalElement; }; Matrix::AccessIllegalElement{}; } I'm trying to implement the inner class for handling errors Error I get: 'Matrix' is not a class, namespace, or enumeration Plus, if inside AccessIllegalElement I want to write a function that prints the illegal index what is preferable? 1) to define a function that takes one parameter 2) to give every class object a member

Fix use of overloaded operator '+' is ambiguous?

对着背影说爱祢 提交于 2020-06-29 03:57:05
问题 I wrote the following code using C++11 standard: .h file: #include "Auxiliaries.h" class IntMatrix { private: Dimensions dimensions; int *data; public: int size() const; IntMatrix& operator+=(int num); }; Bit I am getting and error saying that: error: use of overloaded operator '+' is ambiguous (with operand types 'const mtm::IntMatrix' and 'int') return matrix+scalar; Any idea of what causes this behaviour and how may I fix it? 回答1: You declared the operators in the mtm namespace so the

How to upload a binary/video file using Python http.client PUT method?

删除回忆录丶 提交于 2020-06-28 05:56:12
问题 I am communicating with an API using HTTP.client in Python 3.6.2. In order to upload a file it requires a three stage process. I have managed to talk successfully using POST methods and the server returns data as I expect. However, the stage that requires the actual file to be uploaded is a PUT method - and I cannot figure out how to syntax the code to include a pointer to the actual file on my storage - the file is an mp4 video file. Here is a snippet of the code with my noob annotations :)

`object.__setattr__(self, …, …)` instead of `setattr(self, …, …)`?

旧街凉风 提交于 2020-06-25 18:14:58
问题 Following is the __init__ method of the Local class from the werkzeug library: def __init__(self): object.__setattr__(self, '__storage__', {}) object.__setattr__(self, '__ident_func__', get_ident) I don't understand two things about this code: Why did they write object.__setattr__(self, '__storage__', {}) instead of simply `setattr(self, '__storage__', {})` Why did they even use __setattr__ if the could simply write self.__storage__ = {} 回答1: This ensures that the default Python definition of

Python: Hack to call a method on an object that isn't of its class

谁说胖子不能爱 提交于 2020-06-17 03:18:30
问题 Assume you define a class, which has a method which does some complicated processing: class A(object): def my_method(self): # Some complicated processing is done here return self And now you want to use that method on some object from another class entirely. Like, you want to do A.my_method(7) . This is what you'd get: TypeError: unbound method my_method() must be called with A instance as first argument (got int instance instead) . Now, is there any possibility to hack things so you could

Is it possible to transform default class dunder methods into class methods?

半城伤御伤魂 提交于 2020-06-17 02:52:47
问题 To give you some context, yesterday I came across this post. I found the problem quite interesting, so I tried to find a solution that would keep the syntax as close as possible to what was asked. Here is what I came up with: class DummyCube: cubes = [] @classmethod def __getattribute__(cls, name): print('is this called?') attributes = [getattr(cube, name) for cube in cls.cubes] return attributes class Cube: def __init__(self, volume): DummyCube.cubes.append(self) self.volume = volume a =

Writing a static method in Java to return a string using loops

半世苍凉 提交于 2020-06-16 06:39:31
问题 The full assignment is: Write two public classes (named exactly), TextBox and TextBoxTester. TextBox contains the following overloaded static methods called textBoxString. This method returns a String value. public static String textBoxString (int side) The returned String value, when printed, displays as the outline of a square of side characters. The character you use is up to you. Don't forget that '\n' will force a newline character into the returned String. For example, let's assume I

Java Best way to throw exception in a method

不打扰是莪最后的温柔 提交于 2020-06-09 06:19:11
问题 I have created my own type of exception and want to implement it in a method. As of now I have written it in the following way, and it works. public Worker remove (String firstName, String lastName, String number) throws NoSuchEmployeeException { Worker w = null; for (int i = 0; i < list.size(); i++) { if (list.get(i).getFirstName().compareTo(firstName) == 0 && list.get(i).getLastName().compareTo(lastName) == 0 && list.get(i).getNumber().compareTo(number) == 0) { w = list.get(i); list.remove

Is there a way to call a method on definition of a subclass in Python?

隐身守侯 提交于 2020-05-29 05:17:28
问题 The __init__ method defines what is done on creating an instance of a class. Can I do something equivalent when a subclass is created? Let's say I have the abstract class Entity : class Entity: def __onsubclasscreation__(cls): for var in cls.__annotations__: cls.__dict__[var] = property(lambda self:self.vars[var]) This would mean that whenever I define a new class inheriting from Entity , all annotated variables of that class would receive a getter: class Train(Entity): wagons: int color: str

Is there a way to call a method on definition of a subclass in Python?

天涯浪子 提交于 2020-05-29 05:15:29
问题 The __init__ method defines what is done on creating an instance of a class. Can I do something equivalent when a subclass is created? Let's say I have the abstract class Entity : class Entity: def __onsubclasscreation__(cls): for var in cls.__annotations__: cls.__dict__[var] = property(lambda self:self.vars[var]) This would mean that whenever I define a new class inheriting from Entity , all annotated variables of that class would receive a getter: class Train(Entity): wagons: int color: str