Search Results

Search found 3956 results on 159 pages for 'constructor overloading'.

Page 4/159 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Issue calling superclass method in subclass constructor

    - by stormin986
    I get a NullPointerException calling a Superclass Method in Subclass Inner Class Constructor... What's the Deal? In my application's main class (subclass of Application), I have a public inner class that simply contains 3 public string objects. In the parent class I declare an object of that inner class. public class MainApplication extends Application { public class Data { public String x; public String y; public String z; } private Data data; MainApplication() { data = new Data() data.x = SuperClassMethod(); } } After I instantiate the object in the constructor, I get a runtime error when I try to assign a value in the inner class with a superclass method. Any idea what's up here?? Can you not call superclass methods in the subclass constructor? ** Edit ** Original question was about inner class member assignment in outer class constructor. Turned out the issue was with calling a superclass method in the class's constructor. It was giving me a null pointer exception. Thus, the question has changed.

    Read the article

  • PHP Overloading, singleton instance

    - by jamalali81
    I've sort of created my own MVC framework and am curious as to how other frameworks can send properties from the "controller" to the "view". Zend does something along the lines of $this->view->name = 'value'; My code is: file: services_hosting.php class services_hosting extends controller { function __construct($sMvcName) { parent::__construct($sMvcName); $this->setViewSettings(); } public function setViewSettings() { $p = new property; $p->banner = '/path/to/banners/home.jpg'; } } file: controller.php class controller { public $sMvcName = "home"; function __construct($sMvcName) { if ($sMvcName) { $this->sMvcName = $sMvcName; } include('path/to/views/view.phtml'); } public function renderContent() { include('path/to/views/'.$this->sMvcName.'.phtml'); } } file: property.php class property { private $data = array(); protected static $_instance = null; public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; } public function __set($name, $value) { $this->data[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->data)) { return $this->data[$name]; } } public function __isset($name) { return isset($this->data[$name]); } public function __unset($name) { unset($this->data[$name]); } } In my services_hosting.phtml "view" file I have: <img src="<?php echo $this->p->banner ?>" /> This just does not work. Am I doing something fundamentally wrong or is my logic incorrect? I seem to be going round in circles at the moment. Any help would be very much appreciated.

    Read the article

  • Dynamic Operator Overloading on dict classes in Python

    - by Ishpeck
    I have a class that dynamically overloads basic arithmetic operators like so... import operator class IshyNum: def __init__(self, n): self.num=n self.buildArith() def arithmetic(self, other, o): return o(self.num, other) def buildArith(self): map(lambda o: setattr(self, "__%s__"%o,lambda f: self.arithmetic(f, getattr(operator, o))), ["add", "sub", "mul", "div"]) if __name__=="__main__": number=IshyNum(5) print number+5 print number/2 print number*3 print number-3 But if I change the class to inherit from the dictionary (class IshyNum(dict):) it doesn't work. I need to explicitly def __add__(self, other) or whatever in order for this to work. Why?

    Read the article

  • Overloading stream insertion without violating information hiding?

    - by Chris
    I'm using yaml-cpp for a project. I want to overload the << and >> operators for some classes, but I'm having an issue grappling with how to "properly" do this. Take the Note class, for example. It's fairly boring: class Note { public: // constructors Note( void ); ~Note( void ); // public accessor methods void number( const unsigned long& number ) { _number = number; } unsigned long number( void ) const { return _number; } void author( const unsigned long& author ) { _author = author; } unsigned long author( void ) const { return _author; } void subject( const std::string& subject ) { _subject = subject; } std::string subject( void ) const { return _subject; } void body( const std::string& body ) { _body = body; } std::string body( void ) const { return _body; } private: unsigned long _number; unsigned long _author; std::string _subject; std::string _body; }; The << operator is easy sauce. In the .h: YAML::Emitter& operator << ( YAML::Emitter& out, const Note& v ); And in the .cpp: YAML::Emitter& operator << ( YAML::Emitter& out, const Note& v ) { out << v.number() << v.author() << v.subject() << v.body(); return out; } No sweat. Then I go to declare the >> operator. In the .h: void operator >> ( const YAML::Node& node, Note& note ); But in the .cpp I get: void operator >> ( const YAML::Node& node, Note& note ) { node[0] >> ? node[1] >> ? node[2] >> ? node[3] >> ? return; } If I write things like node[0] >> v._number; then I would need to change the CV-qualifier to make all of the Note fields public (which defeats everything I was taught (by professors, books, and experience))) about data hiding. I feel like doing node[0] >> temp0; v.number( temp0 ); all over the place is not only tedious, error-prone, and ugly, but rather wasteful (what with the extra copies). Then I got wise: I attempted to move these two operators into the Note class itself, and declare them as friends, but the compiler (GCC 4.4) didn't like that: src/note.h:44: error: ‘YAML::Emitter& Note::operator<<(YAML::Emitter&, const Note&)’ must take exactly one argument src/note.h:45: error: ‘void Note::operator(const YAML::Node&, Note&)’ must take exactly one argument Question: How do I "properly" overload the >> operator for a class Without violating the information hiding principle? Without excessive copying?

    Read the article

  • C++ overloading virtual = operator

    - by taz
    Hello, here is the code for my question: class ICommon { public: virtual ICommon& operator=(const ICommon & p)const=0; }; class CSpecial : public ICommon { public: CSpecial& operator=(const CSpecial & cs) { //custom operations return *this; } }; CSpecial obj; Basically: I want the interface ICommon to force it's descendants to implement = operator but don't want to have any typecasts in the implementation. The compiler says "can't instantiate an abstract class. Any help/advice will be appreciated.

    Read the article

  • Function overloading in C

    - by Andrei Ciobanu
    Today, looking at the man page for open(), I've noticed this function is 'overloaded': int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); I didn't thought it's possible on C. What's the 'trick' for achieving this ?

    Read the article

  • C++ function overloading and dynamic binding compile problem

    - by Olorin
    #include <iostream> using namespace std; class A { public: virtual void foo(void) const { cout << "A::foo(void)" << endl; } virtual void foo(int i) const { cout << i << endl; } virtual ~A() {} }; class B : public A { public: void foo(int i) const { this->foo(); cout << i << endl; } }; class C : public B { public: void foo(void) const { cout << "C::foo(void)" << endl; } }; int main(int argc, char ** argv) { C test; test.foo(45); return 0; } The above code does not compile with: $>g++ test.cpp -o test.exe test.cpp: In member function 'virtual void B::foo(int) const': test.cpp:17: error: no matching function for call to 'B::foo() const' test.cpp:17: note: candidates are: virtual void B::foo(int) const test.cpp: In function 'int main(int, char**)': test.cpp:31: error: no matching function for call to 'C::foo(int)' test.cpp:23: note: candidates are: virtual void C::foo() const It compiles if method "foo(void)" is changed to "goo(void)". Why is this so? Is it possible to compile the code without changing the method name of "foo(void)"? Thanks.

    Read the article

  • Does C# allow method overloading, PHP style (__call)?

    - by mr.b
    In PHP, there is a special method named __call($calledMethodName, $arguments), which allows class to catch calls to non-existing methods, and do something about it. Since most of classic languages are strongly typed, compiler won't allow calling a method that does not exist, I'm clear with that part. What I want to accomplish (and I figured this is how I would do it in PHP, but C# is something else) is to proxy calls to a class methods and log each of these calls. Right now, I have code similar to this: class ProxyClass { static logger; public AnotherClass inner { get; private set; } public ProxyClass() { inner = new AnotherClass(); } } class AnotherClass { public void A() {} public void B() {} public void C() {} // ... } // meanwhile, in happyCodeLandia... ProxyClass pc = new ProxyClass(); pc.inner.A(); pc.inner.B(); // ... So, how can I proxy calls to an object instance in extensible way? Extensible, meaning that I don't have to modify ProxyClass whenever AnotherClass changes. In my case, AnotherClass can have any number of methods, so it wouldn't be appropriate to overload or wrap all methods to add logging. I am aware that this might not be the best approach for this kind of problem, so if anyone has idea what approach to use, shoot. Thanks!

    Read the article

  • C++ Beginner - 'friend' functions and << operator overloading: What is the proper way to overload an

    - by Francisco P.
    Hello, everyone! In a project I'm working on, I have a Score class, defined below in score.h. I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is returned. Here's what I tried to do: ostream & Score::operator<< (ostream & os, Score right) { os << right.getPoints() << " " << right.scoreGetName(); return os; } Here are the errors returned: 1>c:\users\francisco\documents\feup\1a2s\prog\projecto3\projecto3\score.h(30) : error C2804: binary 'operator <<' has too many parameters (This error appears 4 times, actually) I managed to get it working by declaring the overload as a friend function: friend ostream & operator<< (ostream & os, Score right); And removing the Score:: from the function declaration in score.cpp (effectively not declaring it as a member). Why does this work, yet the code describe above doesn't? Thanks for your time! Below is the full score.h /////////////////////////////////////////////////////////// // Score.h // Implementation of the Class Score // Created on: 10-Mai-2010 11:43:56 // Original author: Francisco /////////////////////////////////////////////////////////// #ifndef SCORE_H_ #define SCORE_H_ #include <string> #include <iostream> #include <iostream> using std::string; using std::ostream; class Score { public: Score(string name); Score(); virtual ~Score(); void addPoints(int n); string scoreGetName() const; int getPoints() const; void scoreSetName(string name); bool operator>(const Score right) const; ostream & operator<< (ostream & os, Score right); private: string _name; int _points; }; #endif

    Read the article

  • Operator Overloading in C++ as int + obj

    - by Azher
    Hi Guys, I have following class:- class myclass { size_t st; myclass(size_t pst) { st=pst; } operator int() { return (int)st; } int operator+(int intojb) { return int(st) + intobj; } }; this works fine as long as I use it like this:- char* src="This is test string"; int i= myclass(strlen(src)) + 100; but I am unable to do this:- int i= 100+ myclass(strlen(src)); Any idea, how can I achieve this?? Thanks in advance. Regards,

    Read the article

  • C++ superclass constructor calling rules

    - by levik
    What are the C++ rules for calling the superclass constructor from a subclass one?? For example I know in Java, you must do it as the first line of the subclass constructor (and if you don't an implicit call to a no-arg super constructor is assumed - giving you a compile error if that's missing).

    Read the article

  • C++ operator overloading doubt

    - by avd
    I have a code base, in which for Matrix class, these two definitions are there for () operator: template <class T> T& Matrix<T>::operator() (unsigned row, unsigned col) { ...... } template <class T> T Matrix<T>::operator() (unsigned row, unsigned col) const { ...... } One thing I understand is that the second one does not return the reference but what does const mean in the second declaration. Also which function is called when I do say mat(i,j)

    Read the article

  • c++ function overloading, making fwrite/fread act like PHP versions

    - by Newbie
    I'm used to the PHP fwrite/fread parameter orders, and i want to make them the same in C++ too. I want it to work with char and string types, and also any data type i put in it (only if length is defined). I am total noob on c++, this is what i made so far: size_t fwrite(FILE *fp, const std::string buf, const size_t len = SIZE_MAX){ if(len == SIZE_MAX){ return fwrite(buf.c_str(), 1, buf.length(), fp); }else{ return fwrite(buf.c_str(), 1, len, fp); } } size_t fwrite(FILE *fp, const void *buf, const size_t len = SIZE_MAX){ if(len == SIZE_MAX){ return fwrite((const char *)buf, 1, strlen((const char *)buf), fp); }else{ return fwrite(buf, 1, len, fp); } } Should this work just fine? And how should this be done if i wanted to do it the absolutely best possible way?

    Read the article

  • overloading "<<" with a struct (no class) cout style

    - by monkeyking
    I have a struct that I'd like to output using either 'std::cout' or some other output stream. Is this possible without using classes? Thanks #include <iostream> #include <fstream> template <typename T> struct point{ T x; T y; }; template <typename T> std::ostream& dump(std::ostream &o,point<T> p) const{ o<<"x: " << p.x <<"\ty: " << p.y <<std::endl; } template<typename T> std::ostream& operator << (std::ostream &o,const point<T> &a){ return dump(o,a); } int main(){ point<double> p; p.x=0.1; p.y=0.3; dump(std::cout,p); std::cout << p ;//how? return 0; } I tried different syntax' but I cant seem to make it work.

    Read the article

  • Operator Overloading for Queue C++

    - by Josh
    I was trying to use the overload operator method to copy the entries of one queue into another, but I am going wrong with my function. I don't know how else to access the values of the queue "original" any other way than what I have below: struct Node { int item; Node* next; }; class Queue { public: [...] //Extra code here void operator = (const Queue &original); protected: Node *front, *end; } void Queue::operator=(const Queue &original) { //THIS IS WHERE IM GOING WRONG while(original.front->next != NULL) { front->item = original.front->item; front->next = new Node; front = front->next; original.front = original.front->next; } }

    Read the article

  • In Java, is it possible for a super constructor invocation actually invoke a constructor in the calling class?

    - by John Assymptoth
    Super constructor invocation definition: [Primary.] [NonWildTypeArguments] super ( ArgumentListopt ) ; A super constructor call can be prefixed by an Primary expression. Example (taken from JLS): class Outer { class Inner{ } } class ChildOfInner extends Outer.Inner { ChildOfInner() { (new Outer()).super(); // (new Outer()) is the Primary } } Does a Primary expression exist that makes the call to super() the invocation of a constructor of the calling class? Or Java prevents that?

    Read the article

  • Overloading + to add two pointers

    - by iAdam
    I have a String class and I want to overload + to add two String* pointers. something like this doesn't work: String* operator+(String* s1, String* s2); Is there any way to avoid passing by reference. Consider this example: String* s1 = new String("Hello"); String* s2 = new String("World"); String* s3 = s1 + s2; I need this kind of addition to work. Please suggest.

    Read the article

  • Haskell: Problems with overloading: Interpreter can´t tell which + to use

    - by Ben
    Hi, I want to make functions Double - Double an instance of the Num typeclass. I want to define the sum of two functions as sum of their images. So I wrote instance Num Function where f + g = (\ x - (f x) + (g x)) Here the compiler complains he can´t tell whether I´m using Prelude.+ or Module.+ in the lambda expression. So I imported Prelude qualified as P and wrote instance Num Function where f + g = (\ x - (f x) P.+ (g x)) This compiles just fine, but when I try to add two functions in GHCi the interpreter complains again he can´t tell whether I´m using Prelude.+ or Module.+. Is there any way I can solve this problem?

    Read the article

  • Quick, Beginner C++ Overloading Question - Getting the compiler to perceive << is defined for a spec

    - by Francisco P.
    Hello everyone. I edited a post of mine so I coul I overloaded << for a class, Score (defined in score.h), in score.cpp. ostream& operator<< (ostream & os, const Score & right) { os << right.getPoints() << " " << right.scoreGetName(); return os; } (getPoints fetches an int attribute, getName a string one) I get this compiling error for a test in main(), contained in main.cpp binary '<<' : no operator found which takes a right-hand operand of type 'Score' (or there is no acceptable conversion) How come the compiler doesn't 'recognize' that overload as valid? (includes are proper) Thanks for your time.

    Read the article

  • Strange overloading rules in C++

    - by bucels
    I'm trying to compile this code with GCC 4.5.0: #include <algorithm> #include <vector> template <typename T> void sort(T, T) {} int main() { std::vector<int> v; sort(v.begin(), v.end()); } But it doesn't seem to work: $ g++ -c nm.cpp nm.cpp: In function ‘int main()’: nm.cpp:9:28: error: call of overloaded ‘sort(std::vector<int>::iterator, std::vector<int>::iterator)’ is ambiguous nm.cpp:4:28: note: candidates are: void sort(T, T) [with T = __gnu_cxx::__normal_iterator<int*, std::vector<int> >] /usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/bits/stl_algo.h:5199:69: note: void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >] Comeau compiles this code without errors. (4.3.10.1 Beta2, strict C++03, no C++0x) Is this valid C++?

    Read the article

  • Why this function overloading is not working?

    - by Jack
    class CConfFile { public: CConfFile(const std::string &FileName); ~CConfFile(); ... std::string GetString(const std::string &Section, const std::string &Key); void GetString(const std::string &Section, const std::string &Key, char *Buffer, unsigned int BufferSize); ... } string CConfFile::GetString(const string &Section, const string &Key) { return GetKeyValue(Section, Key); } void GetString(const string &Section, const string &Key, char *Buffer, unsigned int BufferSize) { string Str = GetString(Section, Key); // *** ERROR *** strncpy(Buffer, Str.c_str(), Str.size()); } Why do I get an error too few arguments to function ‘void GetString(const std::string&, const std::string&, char*, unsigned int)' at the second function ? Thanks

    Read the article

  • C++ template overloading - wrong function called

    - by DeadMG
    template<typename T> T* Push(T* ptr); template<typename T> T* Push(T& ref); template<typename T, typename T1> T* Push(T1&& ref); I have int i = 0; Push<int>(i); But the compiler calls it ambiguous. How is that ambiguous? The second function is clearly the preferred match since it's more specialized. Especially since the T1&& won't bind to an lvalue unless I explicitly forward/move it. Sorry - i is an int. Otherwise, the question would make no sense, and I thought people would infer it since it's normally the loop iterator.

    Read the article

  • Overloading methods in C#

    - by Craig Johnston
    Is there a way to simplify the process of adding an overloaded method in C# using VS2005? In VB6, I would have just added an Optional parameter the function, but in C# do I have to have to type out a whole new method with this new parameter?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >