473,394 Members | 1,773 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,394 software developers and data experts.

classes, strings, learning in VS.NET

Hi,
I'm just learning c++ with the book from Bjarne Stroustrup and I just type
what is in his book into Visual Studio .NET. Is that a good compiler for
that or not? I'm beginning to have my doubts... and it needs to be ANSI C++
without the .NET framework.

anyway, what's wrong with this:
=====================>
class Employee {
short dept;
public:
Employee();
};

void main(){
Employee e = Employee();
}
<=================
I just put this in 1 file and build, VS complains:

error LNK2001: unresolved external symbol "public: __thiscall
Employee::Employee(void)" (??0Employee@@$$FQAE@XZ)
fatal error LNK1120: 1 unresolved externals

TIA
Jul 22 '05 #1
15 1396
An Ony wrote:

Is that really your name? And your email address is invalid...
I'm just learning c++ with the book from Bjarne Stroustrup and I just type
what is in his book into Visual Studio .NET. Is that a good compiler for
that or not? I'm beginning to have my doubts... and it needs to be ANSI C++
It is a good Compiler, at least the 2003 version.
anyway, what's wrong with this:
=====================>
class Employee {
short dept;
public:
Employee();
Here you declare that you will provide a constructor for your Employee
class.

Later, the linker informs you that it couldnt find one. So either remove
this line and the compiler generates a Constructor automatically for you
or keep your promise and implement it:

Employee::Employee() {
// Do whatever is useful here
}
};

void main(){
int main() {

void is not allowed for main.
Employee e = Employee();
Looks weird. Why not simply:

Employee e;
}


Hope this helps,

Christoph
Jul 22 '05 #2
> > I'm just learning c++ with the book from Bjarne Stroustrup and I just
type
what is in his book into Visual Studio .NET. Is that a good compiler for
that or not? I'm beginning to have my doubts... and it needs to be ANSI
C++
It is a good Compiler, at least the 2003 version.
anyway, what's wrong with this:
=====================>
class Employee {
short dept;
public:
Employee();


Here you declare that you will provide a constructor for your Employee
class.

Later, the linker informs you that it couldnt find one. So either remove
this line and the compiler generates a Constructor automatically for you
or keep your promise and implement it:

Employee::Employee() {
// Do whatever is useful here
}
};

void main(){


int main() {

void is not allowed for main.
Employee e = Employee();


Looks weird. Why not simply:

Employee e;
}

ah thanks, I'll go try it now. I'm used to program in Java so that's how I
thought it was logic. And that book said, "don't declare without
initialization", so isn't "Employee e;" kinda wrong?
Jul 22 '05 #3
ok it works, thx!!

now another problem:

I've got a file X.cpp and an A.cpp and B.cpp
X is the base class and class A and class B are derived from class X (class
A: public X)
so in A.cpp and B.cpp I write: #include "X.cpp"
then in my main.cpp I write #include "A.cpp" and #include "B.cpp"
and trouble strikes: X: class type redefinition

Jul 22 '05 #4

"An Ony" <no*****@somewhere.net> wrote in message
news:bq**********@gaudi2.UGent.be...
ok it works, thx!!

now another problem:

I've got a file X.cpp and an A.cpp and B.cpp
X is the base class and class A and class B are derived from class X (class A: public X)
so in A.cpp and B.cpp I write: #include "X.cpp"
then in my main.cpp I write #include "A.cpp" and #include "B.cpp"
and trouble strikes: X: class type redefinition


Slow down :-) Include statements are for header files and should not be used
for implementations (except templates but this is another story). So
normally you would have a header files where you put all the class
declarations and a .cpp file (or .cxx or .cc or whatever extension you
prefer) where you put the implementation. Furthermore it's good practice to
use "include guards" in your header file to prevent multiple inclusion.

A short example:

Header file MyClassA.h
====================

class CMyClassA {
public:
CMyClassA();
int GetData() const;

protected:
int m_Data;
};

Implementation file MyClassA.cpp:
==============================

#include "MyClassA.h"

CMyClassA()::CMyClassA()
{
m_Data = 0;
}

int CMyClassA::GetData() const
{
return m_Data;
}

HTH
Chris
Jul 22 '05 #5
> ok it works, thx!!

now another problem:

I've got a file X.cpp and an A.cpp and B.cpp
X is the base class and class A and class B are derived from class X (class A: public X)
so in A.cpp and B.cpp I write: #include "X.cpp"
then in my main.cpp I write #include "A.cpp" and #include "B.cpp"
and trouble strikes: X: class type redefinition


I will discuss a technique to avoid redefinition errors at the end of
this posting.

But first of all you should not include .cpp files!

Secondly, in C++ it is common to separate the class definition and the
class implementation. Though the Java way of merging the class
definition and implementation into one file is possible in C++, it is
not recommended way of doing things.

The class definition usually goes into a header file with a .h
extension. In some environments a different extension is used for header
files, but on VS the .h extension is the most common.

A class definition looks something like this:

--------- X.h ---------
class X
{
public:
virtual void foo();
};

The class implementation goes into the .cpp file. These files are
officially called translation units. Translation units serve as input
for C++ compiler. The compiler processes only one translation unit at a
time. Even though your project (or makefile) may consist of many .cpp
files, it is important to understand that the compilation process of the
..cpp files is completely independent from each other. The fact that .cpp
files are compiled separately is nowadays hidden by modern IDE's,
nevertheless it is still important to understand this aspect of the C++
compilation process.

If you want to use a class, derive from it or implement its member
functions, the class definition (that what is stored in the .h file)
must be known to the compiler.

The class implementation file of the X class could look something like
this:

--------- X.cpp ---------
#include "X.h"
#include <iostream>

void X::foo()
{
std::cout << "void X::foo() called" << std::endl;
}

The compiler does not need the class implementation to be able to call
functions on that class or to derive from it. This is the reason why you
should include .h files instead of .cpp files.

Example:
--------- main.cpp ---------
#include "X.h"

int main()
{
X x;
x.foo();
return 0;
}

When compiling the main.cpp file the preprocessor will substitute the
line #include "X.h" with the contents of the X.h file. This way the
compiler can "see" the definition of the X class when compiling the
main.cpp file. When main.cpp is compiled, the compiled code goes into
the main.obj, the compiled code references a X::foo() function, but at
this stage that reference is not resolved. After the X.cpp file is
compiled the compiled code for the X::foo() function can be found in the
X.obj file. The linker puts the relevant code from main.obj and X.obj
into the executable file and resolves the call main() makes to X::foo().

(note that the linking process and .obj file extensions are VS specific
and are beyond the scope of the C++ standard)

To define a derived class the definition of the base class is also
needed:

--------- A.h ---------
#include "X.h"
class A : public X
{
public:
virtual void foo();
};

The implementation file of class A only has to include A.h because the
definition of class X will be include via the A.h file:

--------- A.cpp ---------
#include "A.h"
#include <iostream>

void A::foo()
{
std::cout << "void A::foo() called" << std::endl;
}

A common problem with include files is that directly or indirectly the
include file is included twice within the same translation unit.
Compilers don't like this because it means that they will see
definitions twice or more; there you have your "redefinition error". For
example in the example below X.h is included directly and indirectly via
A.h

--------- main.cpp ---------
#include "X.h"
#include "A.h"

int main()
{
X x;
x.foo();
return 0;
}

A common solution for this problem is putting include guards in the .h
files:

--------- X.h ---------
#ifndef X_H
#define X_H

class X
{
public:
virtual void foo();
};

#endif

When the X.h file is seen once inside a translation unit the next time
it gets included it will skip the definitions and declarations inside
the X.h file. Most experienced C++ programmers will always put include
guards in header files, even when leaving those out would not cause
redefinitions errors at that point in time.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 22 '05 #6
--------- main.cpp ---------
#include "X.h"
#include "A.h"

int main()
{
X x;
x.foo();
return 0;
}

A common solution for this problem is putting include guards in the .h
files:

--------- X.h ---------
#ifndef X_H
#define X_H

class X
{
public:
virtual void foo();
};

#endif

When the X.h file is seen once inside a translation unit the next time
it gets included it will skip the definitions and declarations inside
the X.h file. Most experienced C++ programmers will always put include
guards in header files, even when leaving those out would not cause
redefinitions errors at that point in time.


Since the OP is using VS, he could merely have
#pragma once
at the top of the headers, thus not needing to check #ifdef etc.
Jul 22 '05 #7
> > --------- X.h ---------
#ifndef X_H
#define X_H

class X
{
public:
virtual void foo();
};

#endif

When the X.h file is seen once inside a translation unit the next time it gets included it will skip the definitions and declarations inside the X.h file. Most experienced C++ programmers will always put include guards in header files, even when leaving those out would not cause
redefinitions errors at that point in time.


Since the OP is using VS, he could merely have
#pragma once
at the top of the headers, thus not needing to check #ifdef etc.


Since we are discussing standard C++ here, I prefer to stay as close to
standard C++ as possible. The C++ standard does not specify what a
compiler should do when it encounters #pragma once, other than ignoring
it in case the compiler does not recognize it. Many compilers do not
support this #pragma, and consequently putting #pragma once on top of
the header will not result in the desired behaviour with those
compilers. The solution I proposed should work on any reasonably
conforming compiler.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #8
Allan Bruce wrote:
Since the OP is using VS, he could merely have
#pragma once
at the top of the headers, thus not needing to check #ifdef etc.


But the #ifdef is the normal and correct way to do this. The pragma you
stated is MS specific and only works with their compiler.

Dont you agree that its better to teach the OP to program in Standard
C++ than in a compiler specific dialect?

have a nice day,

Christoph
Jul 22 '05 #9
An Ony wrote:
Employee e = Employee();
Looks weird. Why not simply:

Employee e;
}

ah thanks, I'll go try it now. I'm used to program in Java so that's how I


Yes, I already thought so and was tempted to ask you :)
thought it was logic. And that book said, "don't declare without
initialization", so isn't "Employee e;" kinda wrong?


This would be a definition without initialisation, not a declaration.
The difference between declaration and definition is rather important in
C++.

Nonetheless:

When you define a build-in type:

int i;

i has a random value. This can be very evil if you use this random value
afterwards.

Employee e;

Now, with classes its different. The constructor of e is always called
and it is responsible that the object is in a reliable state afterwards.
So, e is always initialized.
You are quite right, the above notation would be logical (and it is
allowed) but its far more too write ;-)

hth

Christoph
Jul 22 '05 #10

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:3f***********************@aconews.univie.ac.a t...
An Ony wrote:
Employee e = Employee();

Looks weird. Why not simply:

Employee e;

}
ah thanks, I'll go try it now. I'm used to program in Java so that's how

I
Yes, I already thought so and was tempted to ask you :)
thought it was logic. And that book said, "don't declare without
initialization", so isn't "Employee e;" kinda wrong?


This would be a definition without initialisation, not a declaration.
The difference between declaration and definition is rather important in
C++.

Nonetheless:

When you define a build-in type:

int i;

i has a random value. This can be very evil if you use this random value
afterwards.

Employee e;

Now, with classes its different. The constructor of e is always called
and it is responsible that the object is in a reliable state afterwards.
So, e is always initialized.
You are quite right, the above notation would be logical (and it is
allowed) but its far more too write ;-)

hth

Christoph


If you alternatively had

Employee *e;

then this is declared, but not initialised with the constructor until you
call

e = new Employee();

HTH
Allan
Jul 22 '05 #11

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:bq**********@news.freedom2surf.net...
[SNIP]
Since the OP is using VS, he could merely have
#pragma once
at the top of the headers, thus not needing to check #ifdef etc.


This is certainly true. The advantage of #pragma once is that the compiler
does not need to scan the file completely and therefore you gain some speed.
But it's a compiler extension (AFAIK not a standard) and the canonical way
is still to use the include guards which are supported by every compiler &
also by C compilers.

Regards
Chris
Jul 22 '05 #12
In article <bq**********@gaudi2.UGent.be>,
An Ony <no*****@somewhere.net> wrote:
Hi,
I'm just learning c++ with the book from Bjarne Stroustrup and I just type
what is in his book into Visual Studio .NET.
[snip]
void main(){


Just out of curiosity, where does Bjarne Stroustrup use (or advise you to
use) void main()?

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #13
Allan Bruce wrote:
If you alternatively had

Employee *e;

then this is declared, but not initialised with the constructor until you


No it is a definition of a pointer to Employee without
initialisation. It is not a declaration.
e.g.

extern int i;

is a declaration.
Regards,

Christoph

Jul 22 '05 #14
> Allan Bruce wrote:
> Since the OP is using VS, he could merely have
#pragma once
at the top of the headers, thus not needing to check #ifdef etc.


But the #ifdef is the normal and correct way to do this. The pragma you
stated is MS specific and only works with their compiler.

Dont you agree that its better to teach the OP to program in Standard
C++ than in a compiler specific dialect?


indeed :)
Jul 22 '05 #15
Ah, ok, I started wondering why I should use header files in the first
place.

thx so much!
Jul 22 '05 #16

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

Similar topics

2
by: Zalek Bloom | last post by:
I am learning about difference between String and StringBuffer classes. In a book it sayes that a String class is immutable, that means that one an instance of String class is created, the string...
9
by: cjl | last post by:
Hey all: I am working on a little script that needs to pull the strings out of a binary file, and then manipulate them with python. The command line utility "strings" (part of binutils) has...
2
by: | last post by:
I have this class ------------- class Component { /*class*/ Data *d; /*class*/ Draw *a; }; ------------- from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc from...
4
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but...
4
by: ad | last post by:
I want to use string MyString="Test99"; Strings.Rigt(MyString,2); to get the "22" from my string. But I can't do that with CSharp. Strings is belong to Microsoft.VisualBasic Namespace.
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
9
by: TomC | last post by:
Is there any resource where someone who is familiar with Java might find which .NET classes are similar to classes they are familiar with in Java? For example, I have a project that I'd like to...
2
by: Ben Wheare | last post by:
Hiya, I'm fairly new to C++, and trying to figure out how to do the following: I have a class, strings, which has a private char array. To add to this array, I call the member function...
5
by: andrewmorrey | last post by:
Hello, I've got a VC++ project containing multiple classes and a main function. In one of the class functions, it reads from a text file and places the data into a vector; //...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.