473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

calling inherited constructors

I didn't write ClassA, but I'm trying to inherit from ClassA through
ClassB. I want to pass a string to the ClassA constructor but not sure
I'm calling ClassA's constructor correctly. I get the following g++
error ...

In constructor 'ClassB::ClassB(String&)':
../../../include/ClassB.h:19: error: expected `{' at end of input
ClassB *myClass = new ClassB("myClassName");

// ClassB.h
class ClassB : public ClassA {
ClassB(String &name) : ClassA(name);
}

// ClassB.cpp
ClassB::ClassB(String &name)
{
}

// ClassA constructor which I'm trying to pass the string to ...
ClassA::ClassA (const String & name)

What am I doing wrong here?
Sep 23 '08 #1
4 1956
Joe, G.I. wrote:
I didn't write ClassA, but I'm trying to inherit from ClassA through
ClassB. I want to pass a string to the ClassA constructor but not sure
I'm calling ClassA's constructor correctly. I get the following g++
error ...

In constructor 'ClassB::ClassB(String&)':
../../../include/ClassB.h:19: error: expected `{' at end of input
ClassB *myClass = new ClassB("myClassName");

// ClassB.h
class ClassB : public ClassA {
ClassB(String &name) : ClassA(name);
}
class ClassB : public ClassA
{
ClassB(String &name);
};
// ClassB.cpp
ClassB::ClassB(String &name)
{
}
ClassB::ClassB(String &name)
: ClassA( name )
{
}

--
Ian Collins.
Sep 23 '08 #2
On Mon, 22 Sep 2008 22:58:11 +0000, Joe, G.I. wrote:
>
ClassB *myClass = new ClassB("myClassName");

// ClassB.h
class ClassB : public ClassA {
ClassB(String &name) : ClassA(name);
}
}
// ClassB.cpp
ClassB::ClassB(String &name)
{
}
}

There are so many things wrong here its hard to figure what your trying to
do. First, the >class< declaration in the .h file needs a semi-colon at
the end.

And I think its likely that you don't have matching curley braces.

Ruben
--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 23 '08 #3
On Mon, 22 Sep 2008 22:58:11 +0000, Joe, G.I. wrote:
ClassB(String &name) : ClassA(name);
I think there is no semi-colon on the initialization list because it
should be followed up by ClassB()'s {}'s

It should look something like this in the definition of the class ClassB

Bugtrap::Bugtrap(const string errormsg, const string file, const string line) : runtime_error(errormsg.c_str())
{
cerr << what();
cerr << "\nError file: " << file << " and line: " << line;
cerr << "\n**exiting now**\n";

}


--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 23 '08 #4
"Joe, G.I." <invalid.email@addresswrote in
news:gb*********@news4.newsguy.com:
I didn't write ClassA, but I'm trying to inherit from ClassA through
ClassB. I want to pass a string to the ClassA constructor but not sure
I'm calling ClassA's constructor correctly. I get the following g++
error ...

In constructor 'ClassB::ClassB(String&)':
../../../include/ClassB.h:19: error: expected `{' at end of input
ClassB *myClass = new ClassB("myClassName");

// ClassB.h
class ClassB : public ClassA {
ClassB(String &name) : ClassA(name);
}

// ClassB.cpp
ClassB::ClassB(String &name)
{
}

// ClassA constructor which I'm trying to pass the string to ...
ClassA::ClassA (const String & name)

What am I doing wrong here?
Assuming you fix the minor issues (like the missing semicolon at the end of
the class declaration), the piece you've got wrong is that the initializer
list goes with the constructor definition, not the declaration:

class ClassB : public ClassA {
ClassB(String & name);
};

ClassB::ClassB(String & name) : ClassA(name) {}
Something else to consider... do you really want to pass the String by non-
const reference? Generally speaking, if you're passing stuff by reference,
you should probably pass it by const-reference (unless you have a specific
reason to pass it non-const...)
Sep 23 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SLE | last post by:
Hi there, I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer. ...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
3
by: Alexander Muylaert | last post by:
Hi Is their a way to keep constructors visible in inherited classes? public class X{ public x(int A){} } public class Y : X{};
4
by: Claire | last post by:
I'm having real brain failure today. I've done this lots of times with constructors but not with virtual methods and the compiler complains because Ive put the :base(foo) after the function...
2
by: PIEBALD | last post by:
OK, here's my latest workaround for the lack of inherited constructors... It requires that the class have a parameterless (default) constructor and a public virtual "Initialize" method (which...
3
by: M O J O | last post by:
Hi, I've created a MasterForm which all my forms in my project must derive from. In my MasterForm, I've overloaded the New event with this code: Public Sub New(ByVal SomeText As String)...
20
by: lars.uffmann | last post by:
I have to work on a rather big project that a bunch of people wrote and that has no useful documentation at all, my C++ has rusted in a bit, now I stumbled over a constructor that looks something...
3
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile....
12
by: Oleg Subachev | last post by:
I am moving from Delphi to C# and hve encountered the problem: I have the following classes and form Load event handler: public class class1 { public string S; public class1( string aS ) {...
2
by: Søren M. Olesen | last post by:
Hi How do I create an instance of an object with an inherited cunstructor?? In the below example, I'm able to create an instance of MyClass2 using the forst two lines of code, however if I try...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.