Search Results

Search found 3493 results on 140 pages for 'constructor'.

Page 17/140 | < Previous Page | 13 14 15 16 17 18 19 20 21 22 23 24  | Next Page >

  • Bad Design? Constructor of composition uses `this`

    - by tanascius
    Example: class MyClass { Composition m_Composition; void MyClass() { m_Composition = new Composition( this ); } } I am interested in using depenency-injection here. So I will have to refactor the constructor to something like: void MyClass( Composition composition ) { m_Composition = composition; } However I get a problem now, since the Composition-object relies on the object of type MyClass which is just created. Can a dependency container resolve this? Is it supposed to do so? Or is it just bad design from the beginning on?

    Read the article

  • C++: Avoid .cpp files with only an empty (de)constructor

    - by Martijn Courteaux
    Hi, When I have a header file like this: #ifndef GAMEVIEW_H_ #define GAMEVIEW_H_ #include <SDL/SDL.h> class GameView { public: GameView(); virtual ~GameView(); virtual void Update() = 0; virtual void Render(SDL_Surface* buffer) = 0; }; #endif /* GAMEVIEW_H_ */ I need to create a .cpp file like this: #include "GameView.h" GameView::~GameView() { } GameView::GameView() { } This is a bit stupid. Just a .cpp file for an empty constructor and deconstructor. I want to implement that method simply in the header file. That is much cleaner. How to do this?

    Read the article

  • No Parameterless Constructor defined for - ViewModel with UOW

    - by TheVillageIdiot
    I have a view model class which uses UnitOfWork to some database operations like fetching of items to create select lists and IPrincipal for some auditing (like modified by etc.). It cannot work without this UOW. I have configured my web site to use Ninject to inject UOW into Controllers. From controller I pass this UOW when creating view model. But when performing POST operation I am getting No parameterless constructor defined for this object. I have few SelectList type of properties which I have excluded with Bind attribute. How can I overcome this problem? Can I configure Ninject to create the objects of this type and make ModelBinder use it?

    Read the article

  • C# Generic List constructor gives me a MethodAccessException

    - by evilfred
    Hi, I make a list in my code like so: List<IConnection> connections = new List<IConnection>(); where IConnection is my own interface. This is in a .NET 2.0 executable. If I run the code on my machine (with lots of .Net versions installed) it works fine. If I run it on my test machine (which only has .NET 3.5 SP1 installed) then I get a MethodAccessException in the System.Collections.Generic.List constructor. Any ideas what could be going wrong?

    Read the article

  • Call a C++ constructor from an Objective C class

    - by syvex
    How can I call a C++ constructor from inside an Objective C class? class CppClass { public: CppClass(int arg1, const std::string& arg2): _arg1(arg1), _arg2(arg2) { } // ... private: int _arg1; std::string _arg2; }; @interface ObjC: NSObject { CppClass _cppClass; } @end @implementation ObjC - (id)init { self = [super init]; if ( self ) { // what is the syntax to call CppClass::CppClass(5, "hello") on _cppClass? } return self; } @end

    Read the article

  • C++ Add this pointer to a container by calling it in base class constructor

    - by vivekeviv
    class Base { public: Base (int a, int b); private: int a,b; }; class Derived1 { public: Derived1():base(1,2){} }; similarly Derived2, Derived 3 which doesnt contain any data members on its own Now i need to contain these derived objects in a singleton, so i was thinking to call this in base constructor like Base::Base(int a, int b) { CBaseMgr::GetInstance()->AddtoVector(this); } so now if i construct Derived d1, d2, d3 etc. will the Singleton's container contain all derived objects? My doubt is can i do this adding of objects to container in base ctor or should i do in derived ctor.?

    Read the article

  • C++ Declaring an uninitialized variable without a null constructor

    - by xbonez
    Consider the DUPoint class, whose declaration appears below. Assume this code appears in a file named DUPoint.h: #include <string> class DUPoint { public: DUPoint (int x, int y); int getX () const; int getY () const; void setX (int x); void setY (int y); void print(); private: int x_; int y_; }; Is it true that you cannot declare an uninitialized DUPoint variable with a statement such as DUPoint P; using this class as currently configured because it has no null constructor?

    Read the article

  • objective-c - calling one constructor from another

    - by synic
    Say you had the following two constructors: - (id)initWithTitle:(NSString *)title; - (id)initWithTitle:(NSString *)title page:(NSString *)page; The second constructor is no different from the first, except that it sets up the member variable "page". Since it basically has to do the same thing, is there a way to call the first one from the second one to reduce code duplication, or do you have to set up a third method to do the common tasks? I'm talking about something similar to this, though I doubt this will work: - (id)initWithTitle:(NSString *)_title { if(self = [super init]) { self.title = _title; } return self; } - (id)initWithTitle:(NSString *)_title page:(NSString *)_page { if(self = [self initWithTitle:_title]) { self.page = _page; } return self; }

    Read the article

  • C# Generic new() constructor problem

    - by LB
    I'm trying to create a new object of type T via its constructor when adding to the list. I'm getting a compile error: The correct error message is: 'T': cannot provide arguments when creating an instance of a variable But it does! Any ideas? public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { tabListItems.Add(new T(listItem)); // error here. } ... }

    Read the article

  • Call subclass constructor from abstract class in Java

    - by Joel
    public abstract class Parent { private Parent peer; public Parent() { peer = new ??????("to call overloaded constructor"); } public Parent(String someString) { } } public class Child1 extends parent { } public class Child2 extends parent { } When I construct an instance of Child1, I want a "peer" to automatically be constructed which is also of type Child1, and be stored in the peer property. Likewise for Child2, with a peer of type Child2. The problem is, on the assignment of the peer property in the parent class. I can't construct a new Child class by calling new Child1() because then it wouldn't work for Child2. How can I do this? Is there a keyword that I can use that would refer to the child class? Something like new self()?

    Read the article

  • QObject cloning

    - by Olorin
    I know that Qobjects are supposed to be identities not values eg you cannot copy them and by default the copy constructor and asignment are disabled as explained in qt documentation. But is it possible to create a new Qobject from an existing one using a clone method? Would this be a logic error ? If i say QObject b; QObject a; b.cloneFrom(a); or QObject a = new QBject(); QObject b = new QBject(); b->cloneFrom(a); and the clone method copyes stuff like members etc would this be wrong? And if this is ok can i write my own copy constructor and asignment operator that does just that? Note: i actually want to try this with classes that inherit qobject.

    Read the article

  • Kind of stumped with some basic C# constructors.

    - by Sergio Tapia
    public class Parser { Downloader download = new Downloader(); HtmlDocument Page; public Parser(string MovieTitle) { Page = download.FindMovie(MovieTitle); } public Parser(string ActorName) { Page = download.FindActor(ActorName); } } I want to create a constructor that will allow other developers who use this library to easily create a Parser object with the relevant HtmlDocument already loaded as soon as it's done creating it. The problem lies in that a constructor cannot exist twice with the same type of parameters. Sure I can tell the logical difference between the two paramters, but the computer can't. Any suggestions on how to handle this? Thank you!

    Read the article

  • How can I create two contructors that act differently but recieve the same data type?

    - by Sergio Tapia
    public class Parser { Downloader download = new Downloader(); HtmlDocument Page; public Parser(string MovieTitle) { Page = download.FindMovie(MovieTitle); } public Parser(string ActorName) { Page = download.FindActor(ActorName); } } I want to create a constructor that will allow other developers who use this library to easily create a Parser object with the relevant HtmlDocument already loaded as soon as it's done creating it. The problem lies in that a constructor cannot exist twice with the same type of parameters. Sure I can tell the logical difference between the two paramters, but the computer can't. Any suggestions on how to handle this? Thank you!

    Read the article

  • Question regarding inheritance in wxWidgets.

    - by celestialorb
    Currently I'm attempting to write my own wxObject, and I would like for the class to be based off of the wxTextCtrl class. Currently this is what I have: class CommandTextCtrl : public wxTextCtrl { public: void OnKey(wxKeyEvent& event); private: DECLARE_EVENT_TABLE() }; Then later on I have this line of code, which is doesn't like: CommandTextCtrl *ctrl = new CommandTextCtrl(panel, wxID_ANY, *placeholder, *origin, *size); ...and when I attempt to compile the program I receive this error: error: no matching function for call to ‘CommandTextCtrl::CommandTextCtrl(wxPanel*&, <anonymous enum>, const wxString&, const wxPoint&, const wxSize&)’ It seems that it doesn't inherit the constructor method with wxTextCtrl. Does anyone happen to know why it doesn't inherit the constructor? Thanks in advance for any help!

    Read the article

  • Foo f = Foo(); // no matching function for call to 'Foo::Foo(Foo)' ... huh?!

    - by Kyle
    class Foo { public: explicit Foo() {} explicit Foo(Foo&) {} }; Foo d = Foo(); error: no matching function for call to 'Foo::Foo(Foo)' I tried changing Foo(Foo&) to Foo(Foo) as the error suggests, which AFAIK is not a valid constructor, and sure enough I get: error: invalid constructor; you probably meant ‘Foo (const Foo&)’ What gives? How do I resolve this? (This is on GCC by the way)

    Read the article

  • Java program has errors, 80 lines of code

    - by user2961687
    I have a problem with a program. It contains a lot of errors in Eclipse. Sorry for my english and thank you in advance. Here is the task: I. Declare a class that contains private fields Jam: String taste, double weight Create constructors containing variables as parameters: (String taste, double weight), (double weight), (String taste). Parameters constructors should initialize class fields. In case the constructor does not provide the necessary parameter, it must be assumed that the field taste must have the value "No Name" and weight - 100.0. Introduce the use of all constructors creating objects that represent three kinds of jams. Note: it must be assumed that the only constructor with two parameters can be assigned to fields of the class. Declare a class Jar that contains the field Jam jam, a dedicated constructor initiating all declared fields and methods: open close isItOpen Next, create an object of class Jar and fill it with the selected type of jam, operations repeat for all the kinds of jams. This is my code this far: public class App { public static void main(String[] args) { Jam strawberry = new Jam("strawberry", 20.45); Jam raspberry = new Jam(40.50); Jam peach = new Jam("peach"); Jar jar_1 = new Jar(); Jar jar_2 = new Jar(); Jar jar_3 = new Jar(); jar_1.open(); jar_1.fillIn(strawberry); jar_2.fillIn(peach); jar_3.fillIn(raspberry); } } class Jam { String taste; double weight; public Jam(String taste, double weight) { this.taste = taste; this.weight = weight; } public Jam(double weight) { this.taste = "No name"; this.weight = weight; } public Jam(String taste) { this.taste = taste; this.weight = 100.0; } } class Jar { public Jam Jam = new Jam(); private String state_jar; public Jar() { Jam.weight = 0; Jam.taste = ""; state_jar = "closed"; } public static String open() { state_jar = open; return state_jar; } public static String close() { state_jar = "closed"; return state_jar; } public static boolean isItOpen() { return state_jar; } public void fillIn(Jam jam) { if (isItOpen == false) open(); this.Jam.weight = jam.weight; this.Jam.taste = jam.taste; this.Jam.close(); } }

    Read the article

  • Is it safe to call a function to intialize a class in a ctor list?

    - by Michael Dorgan
    I have Angle class that I want initialized to a random value. The Angle constructor can accept an int from a random() function. Is it safe to place this call in the ctor list: foo::foo() : Angle(random(0xFFFF)) {...} or do I have to do it in the body of the constructor? foo::foo() { Angle = Angle(random(0xFFFF)); ...} If it matters, the foo class is derived from another class and does have virtual methods. In addition, no exception handling is allowed in our app.

    Read the article

  • Custom class object in Initialization list

    - by Michael
    I have a class Bar: class Bar { public: Bar(void); ~Bar(void); }; And a class Foo that gets a reference to Bar object as a constructor parameter and needs to save it in a private member bar_ : class Foo { private: Bar& bar_; public: Foo(Bar& bar) : bar_(bar) {} ~Foo(void) {} }; This doesn't compile : overloaded member function not found in 'Parser' missing type specifier - int assumed. Note: C++ does not support default-int Now i suspect couple of things that i need to assure, the second error is for Bar& bar_; declaration in Foo. Do i need to use an explicit constructor when declaring bar_ ? I am interested in learning how the compiler works regarding this matter, so a detailed explanation would be highly appreciated. Thanks.

    Read the article

  • Static member object of a class in the same class

    - by Luv
    Suppose we have a class as class Egg { static Egg e; int i; Egg(int ii):i(ii) {} Egg(const Egg &); //Prevents copy-constructor to be called public: static Egg* instance() {return &e} }; Egg Egg::e(47); This code guarantees that we cannot create any object, but could use only the static object. But how could we declare static object of the same class in the class. And also one thing more since e is a static object, and static objects can call only static member functions, so how could the constructor been called here for static object e, also its constructors are private.

    Read the article

  • Constructors in Inner classes (implementing Interfaces)

    - by thepandaatemyface
    Hi, How would I go about writing a constructor for an inner class which is implementing an interface? I know I could make a whole new class, but I figure there's got to be a way to do something along the line of this: JButton b = new JButton(new AbstractAction() { public AbstractAction() { super("This is a button"); } public void actionPerformed(ActionEvent e) { System.out.println("button clicked"); } }); When I enter this it doesn't recognize the AbstractAction method as a constructor (compiler asks for return type). Anyone have an idea? Thanks

    Read the article

  • Constructors from extended class in Java

    - by Crystal
    I'm having some trouble with a hw assignment. In one assignment, we had to create a Person class. Mine was: public class Person { String firstName; String lastName; String telephone; String email; public Person() { firstName = ""; lastName = ""; telephone = ""; email = ""; } public Person(String firstName) { this.firstName = firstName; } public Person(String firstName, String lastName, String telephone, String email) { this.firstName = firstName; this.lastName = lastName; this.telephone = telephone; this.email = email; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public boolean equals(Object otherObject) { // a quick test to see if the objects are identical if (this == otherObject) { return true; } // must return false if the explicit parameter is null if (otherObject == null) { return false; } if (!(otherObject instanceof Person)) { return false; } Person other = (Person) otherObject; return firstName.equals(other.firstName) && lastName.equals(other.lastName) && telephone.equals(other.telephone) && email.equals(other.email); } public int hashCode() { return 7 * firstName.hashCode() + 11 * lastName.hashCode() + 13 * telephone.hashCode() + 15 * email.hashCode(); } public String toString() { return getClass().getName() + "[firstName = " + firstName + '\n' + "lastName = " + lastName + '\n' + "telephone = " + telephone + '\n' + "email = " + email + "]"; } } Now we have to extend that class and use that class in our constructor. The function protoype is: public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax, double interestRate, CAR_LOAN_TERMS length) I'm confused on how I use the Person constructor from the superclass. I cannot necessarily do super(client); in my constructor which is what the book did with some primitive types in their example. Not sure what the correct thing to do is... Any thoughts? Thanks!

    Read the article

  • When would I need to call base() in C#?

    - by user310291
    My BaseClass Constructor is called whereas I have a constructor in derived class so when would I need to call base() ? class BaseClass { public BaseClass() { Debug.Print("BaseClass"); } } class InheritedClass : BaseClass { public InheritedClass() { Debug.Print("InheritedClass"); } } private void Form1_Load(object sender, EventArgs e) { InheritedClass inheritedClass = new InheritedClass(); } Output 'Inheritance.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll' 'Inheritance.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. BaseClass InheritedClass The thread 'vshost.RunParkingWindow' (0x12b4) has exited with code 0 (0x0). The thread '<No Name>' (0x85c) has exited with code 0 (0x0). The program '[4368] Inheritance.vshost.exe: Program Trace' has exited with code 0 (0x0). The program '[4368] Inheritance.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

    Read the article

  • error: expected constructor, destructor, or type conversion before '(' token

    - by jonathanasdf
    include/TestBullet.h:12: error: expected constructor, destructor, or type conver sion before '(' token I hate C++ error messages... lol ^^ Basically, I'm following what was written in this post to try to create a factory class for bullets so they can be instantiated from a string, which will be parsed from an xml file, because I don't want to have a function with a switch for all of the classes because that looks ugly. Here is my TestBullet.h: #pragma once #include "Bullet.h" #include "BulletFactory.h" class TestBullet : public Bullet { public: void init(BulletData& bulletData); void update(); }; REGISTER_BULLET(TestBullet); <-- line 12 And my BulletFactory.h: #pragma once #include <string> #include <map> #include "Bullet.h" #define REGISTER_BULLET(NAME) BulletFactory::reg<NAME>(#NAME) #define REGISTER_BULLET_ALT(NAME, CLASS) BulletFactory::reg<CLASS>(NAME) template<typename T> Bullet * create() { return new T; } struct BulletFactory { typedef std::map<std::string, Bullet*(*)()> bulletMapType; static bulletMapType map; static Bullet * createInstance(char* s) { std::string str(s); bulletMapType::iterator it = map.find(str); if(it == map.end()) return 0; return it->second(); } template<typename T> static void reg(std::string& s) { map.insert(std::make_pair(s, &create<T>)); } }; Thanks in advance.

    Read the article

  • DeSerialization doesn't work though i Implement GetObjectData method and Constructor

    - by Punit Singhi
    Hi, I have a static generic dictionary in a class. As static memeber cannot serialized so i have implented ISerializable interface and method GetObjectData to serialize. I have a constructor which will also accept SerializationInfo and StreamingContext to deserliaze the dictionay. Now when i try to serialize and deserialize , it always return 1(thoug i added 2 entries). please find the pseduo code- [Serializable] public class MyClass : ISerializable { internal static Dictionary<long, string> dict = new Dictionary<long,string>(); public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("static.dic", MyClass1.dict, typeof(Dictionary<long, string>)); } public MyClass(SerializationInfo info, StreamingContext context) { MyClass.dict= (Dictionary<long, string>)info.GetValue("static.dic", typeof(Dictionary<long, string>)); } public void Add() { dict.Add(21, "11"); } public MyClass() { dict.Add(21, "11"); } } public class MyClass { MyClass myClass = new MyClass(); public static void Main() { myClass.Add(); FileStream fileStream = new FileStream("test.binary", FileMode.Create); IFormatter bf = new BinaryFormatter(); bf.Serialize(fileStream, myClass); fileStream.Dispose(); fileStream.Close(); fileStream = new FileStream("test.binary", FileMode.Open); bf = new BinaryFormatter(); myClass = (MyClass1)bf.Deserialize(fileStream); } }

    Read the article

< Previous Page | 13 14 15 16 17 18 19 20 21 22 23 24  | Next Page >