I'm pretty sure I'm probably doing something stupid, but I've been at this an hour and a half and can't figure out what I'm missing.
I can create an object from my class using the default constructor, but can't use an overloaded constructor when I add one. I can't call the print member function that I have included or any others that I have tried to include either. I have put the three files into a Code::Blocks project and gotten the same result. I have also tried the three files on Dev-Cpp with the same result. Any help would be greatly appreciated.
Main Function
#include <iostream>
#include "Appt.h"
using namespace std;
int main()
{
Appt a();
a.print();
}
Appt.h
#ifndef APPT_H
#define APPT_H
#include <iostream>
#include <string>
using namespace std;
class Appt
{
public:
Appt();
void print();
private:
string description;
};
#endif // APPT_H
Appt.cpp
#include "Appt.h"
using namespace std;
Appt::Appt()
{
description = "No Description";
}
void Appt::print()
{
cout << description << endl;
}
I am using Code::Blocks 16.01 with the GCC compiler. These files are not currently in a project. I am also running Windows 7.
It looks like your problems may be related to this line:
Appt a();
Unfortunately, while this looks like it calls the default constructor, it actually declares a to be of type Appt(), that is, a function taking no arguments and returning Appt. If you want to call the default constructor, there are a few options:
Appt a;
Appt a = Appt();
Appt a{}; // requires C++11
I would prefer the last one.
Related
All I'm trying to do is create a separate class to hold my Hello World function (this is for a class), but I am getting an "identifier is undefined" compiler error. What is the issue?
Here is my main function (helloworld.cpp):
#include <iostream>
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
And here is the header class (helloworld.h) :
#include <iostream>
void print_me() {
std::cout << "Hello World\n";
}
You have not included helloworld.h in helloworld.cpp. The following code should work:
#include <iostream>
#include "helloworld.h"
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
One thing to remember is from your compiler's point of view, there is no connection between the two files unless you specify it. The fact that both files have the same name does not have any significance for compiler.
Side note 1: Consider using include guards in your header files. For simple projects, it may not be obviously necessary but for larger projects, not using them can lead to annoying ambiguous compilation errors.
Side note 2: Implementing function bodies in header files is generally discouraged.
i'm trying to write my c++ program in different files, but I can't seem to get it to work. can someone help me?
separate.cpp
#include "separate.h"
#include <iostream>
void Separate() {
cout << "text";
}
separate.h
#include <string>
using namespace std;
class OneLine {
Separate();
private:
string vari;
};
main.cpp
#include "separate.cpp"
#include <iostream>
using namespace std;
int main () {
Separate s;
s();
return 0;
}
Two basic errors:
In separate.cpp, you need
void OneLine::Separate() { /*...*/ }
and in main.cpp you want to create an object of your type and call the defined method on it like this:
OneLine ol;
ol.Separate();
For this, you need to make the method public, change separate.h:
class OneLine {
public:
Separate();
//...
};
You want to change a few more things as well which are not needed for this simple example but they will become necessary in the long run:
You want include guards, google for "include guard"
You don't want using namespace std; - get rid of it and add std:: where necessary
In your implementation define the function as:
void OneLine::Separate() {
...
In your main, you need to instantiate a OneLine object and call Separate on that, i.e.:
OneLine o;
o.Separate();
In your main file you need to reference "separate.h" rather than "separate.cpp"
In seperate.cpp the class method needs to be prefixed with the class name:
void Oneline::Separate()
Also you should be creating an object of type OneLine rather than of type Seperate.
So:
Oneline one;
one.Seperate();
I have two libraries included in my program which both have the same function name, but I need to be able to use both, but I also need C++ to know which one I'm referring to (in certain places I will only be referring to one or the other). The reason why I'm doing this is because I am making my own library and I want to have certain names for my functions, but they are conflicting with functions in someone else's library that I've included, and to make matters worse, some of my functions in my library actually USE the functions in the other persons library which has the same name.
My library is just a .h/.cpp file by the way. Also, when calling MY functions, I don't want any extra luggage such as myNameSpace::myFunc(). I just want to call it myFunc(). However, I don't mind calling the other persons function using a namespace (though I don't want to modify their library in case I break something). (I'm completely new to C++ btw)
HERES MY NEW (TEST - SO FAR) CODE : NOT WORKING W/ ERRORS:
error C2668: 'myFunc' : ambiguous call to overloaded function
main program.cpp
#include "otherslib.h"
#include "mylib.h"
#include <iostream>
using namespace myNamespace;
int main(){
std::cout << myFunc() << std::endl;
return 0;
}
mylib.h
#pragma once
namespace myNamespace{
int myFunc();
}
mylib.cpp
#include "mylib.h"
namespace myNamespace{
int myFunc(){
return 1;
}
}
otherslib.h
#pragma once
int myFunc();
otherslib.cpp
#include "otherslib.h"
int myFunc(){
return 0;
}
You should define your functions in a namespace, and use the namespace when calling them.
namespace myNamespace
{
int myFunc(etc) { ... }
}
int main() {
cout << myNamespace::myFunc();
}
To avoid having to specify your namespace all the time, you could do something like this:
namespace myNamespace
{
int myFunc(etc) { ... }
int main()
{
// Call your own myFunc:
myFunc();
// Call their myFunc:
::myFunc();
}
}
I have a class which is going to handle an array of objects of another class I've created earlier (which works fine). The problem appears when I try to create an object of my List-class.
This is the header of the list-class:
#ifndef personlistH
#define personlistH
#include "Person.h"
#include <iomanip>
#include <iostream>
#define SIZE 10
namespace std {
class PersonList {
private:
Person persons[SIZE];
int arrnum;
string filename;
public:
Personlist();
};
}
#endif
This is the main function:
#include <iostream>
#include "PersonList.h"
using namespace std;
int main() {
PersonList personlist;
return 0;
}
The error my compiler is giving me is the following:
error: "27 \PersonList.h ISO C++ forbids declaration of `Personlist'
with no type"
I've searched for answers but as I'm quite new to C++ it's been a bit confusing and I haven't found any fitting yet. It would be great if you could explain this error for me.
You have the wrong capitalisation on your constructor declaration. You have Personlist(); but need PersonList();. Because what you have isn't equal to the class name it is considered a function rather than a constructor, and a function needs a return type.
Do not add your own types to the standard namespace(std), instead create your own namespace and define your class inside it.
//PersonList.h
namespace PersonNamespace
{
class PersonList
{
//members here
};
}
//Main.cpp
using namespace PersonNamespace;
The actual error is that you made a typo in Personlist instead of PersonList
The error is because you got the capitalisation wrong when you declared the constructor; it should be PersonList() not Personlist().
Also, you should never declare your own classes in the std namespace; that's reserved for the standard library. You shoud make up your own namespace name, and put your things in that.
Could any body offer me any reason about that?
If we do it like that, what's the outcome? Compile error?
The problem is that static initialization isnt just initialization, it is also definition. Take for example:
hacks.h :
class Foo
{
public:
static std::string bar_;
};
std::string Foo::bar_ = "Hello";
std::string GimmeFoo();
main.cpp :
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
int main()
{
string s = GimmeFoo();
return 0;
}
foo.cpp :
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
string GimmeFoo()
{
Foo foo;
foo;
string s = foo.bar_;
return s;
}
In this case, you can't initialize Foo::bar_ in the header because it will be allocated in every file that #includes hacks.h. So there will be 2 instances of Foo.bar_ in memory - one in main.cpp, and one in foo.cpp.
The solution is to allocate & initialize in just one place:
foo.cpp :
...
std::string Foo::bar_ = "Hello";
...
It is just a limitation in the language it self. Hopefully, when C++0x becomes reality, this limitation would go away.
I think this page gives a somehow good reason:
One of the trickiest ramifications of
using a static data member in a class
is that it must be initialized, just
once, outside the class definition, in
the source file. This is due to the
fact a header file is typically seen
multiple times by the compiler. If the
compiler encountered the
initialization of a variable multiple
times it would be very difficult to
ensure that variables were properly
initialized. Hence, exactly one
initialization of a static is allowed
in the entire program.