473,729 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::Emplo yee(void)" (??0Employee@@$ $FQAE@XZ)
fatal error LNK1120: 1 unresolved externals

TIA
Jul 22 '05 #1
15 1419
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::Emplo yee() {
// 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::Emplo yee() {
// 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*****@somewh ere.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()::CM yClassA()
{
m_Data = 0;
}

int CMyClassA::GetD ata() 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 "redefiniti on 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.merke rk(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.merke rk(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

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

Similar topics

2
5535
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 it contais cannot be changed, only reference will change to point to a different string. So results will be different in the following code depending is a, b are defined as String or StringBuffer: a = new String("test") (or new...
9
3293
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 exactly the functionality I need, but I was thinking about trying to implement this in pure python. I did some reading on opening and reading binary files, etc., and was
2
4885
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 "Draw" derive classes like "TextBoxDraw", "ButtonDraw", "LabelDraw", "ComboBoxDraw" etc from "Data" derive classes like "TextBoxData", "ButtonData", "LabelData", "ComboBoxData" etc
4
1807
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 understanding others peoples logic isnt (that doesnt mean they are wrong). i prefer designing classes for the currect job and making tangible 'things' classes , not overdoing it with loads of classes or inheritance.. it seems easier to make...
4
8872
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
2376
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 or another (could also be 3) different web servers to use these Web Services. The Web Services are identical on all the machines. I tried just changing the URL of the Web Services and cannot make it work. I then decided to create 2 identical web...
9
2719
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 tackle as a learning project. In Java, I'd subclass the Panel or JPanel class. Is there someplace where a Java programmer who is learning C# could quickly find out what the analog for those classes are?
2
1586
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 add_array(int), with an int, which populates the array (in this case with the binary equivalent of that integer). I want to make another class, messages, which encapsulates the strings
5
2156
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; // std::vector<std::vector<std::string applications (50, std::vector<std::string>(12)); applications = "Test1"; applications = "Test2";
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.