**Its header file**
#ifndef MUSTERI_H
#define MUSTERI_H
#include <string>
using std::string;
class Musteri
{
public:
string ad;
void SetAd(string);
string GetAd();
};
#endif
its cpp file
#include "Musteri.h"
#include <string>
void Musteri::SetAd(string deger)
{
ad=deger;
}
string Musteri::GetAd()
{
return ad;
}
its Main.cpp
int main(int argc, char** argv) {
vector <Musteri> musteriler;
Musteri musteri_x;
musteri_x.SetAd("John");
musteri_x.SetSoyad("Wick");
musteriler.push_back(musteri_x);
musteri_x.SetAd("Jena");
musteri_x.SetSoyad("Rick");
musteriler.push_back(musteri_x);
cout<<musteriler[0].GetAd();//its not working
return 1;
}
cout<<musteriler[0].GetAd()
i cant reach methods like this
please help me.what should i do? how i can reach vector of a class ?
detail detail detail detail detail detail detail detail detail
From the code you have posted (which is apparently incomplete, as your definition of class Musteri does not contain any member function called SetSoyad...) and a quite unclear question, one can only guess what your problem is. What does "I can't reach" mean?
However I have a feeling that you have problem with either header files or namespaces. If you want to use std::vector, you must include the header. Similarly for std::cout you need . Both these names need to be prefixed by std:: since they live in the standard namespace.
Related
First I'm a beginner at programming so don't expect me to understand every code-specific word.
Second I'm sometimes slow on the uptake.
Third I think I covered the Basics of C++ but that's it. I'm happy to learn more of course!
To my Question:
I'm programming a little code to experience with classes. I made two classes, each one in a different .h and .cpp file. Now each one is using the headers iostream and string.
How should I include those without making any Problems? Is #pragma once sufficient?
Second question is about using namespace std:
where should i put it(I know it isn't a bad use but ist only for a small Programm)
First Header:
#pragma once
#include <iostream>
#include <string>
//using namespace std Nr.1
class one
{
};
Second Header:
#pragma once
#include <iostream>
#include <string>
//using namespace std Nr.2
class two
{
};
Finally Main:
#include "one.h"
#include "two.h"
//using namespace std Nr.3
int main()
{
return 1;
};
Thanks in advance for responding.
There's no problem including twice iostream and string in both class headers'.
The #pragma directive is used to protect two declarations of types (typedef, classes) of your own types.
Hence it applies to your class headers'.
Moreover, there are drawbacks using #pragma directive as stated here : https://stackoverflow.com/a/1946730/8438363
I recommend using preprocessor defines guards :
#ifndef __MY_HEADER__
#define __MY_HEADER__
//... your code here
#endif
Hope this helps.
You will need to use Include guards.They ensure the compiler includes each "included" header file(#include "XXX.h") only Once.
But if you are creating a small application & don't mind recompiling/rebuilding your entire project if need be, then putting the common header files in a dedicated header file is fair game & keeps code clean & small.
You can also make a header with all the common dependecies you need and include it in each class that needs those dependecies. Here is a simple example:
Core.h
#include <iostream>
#include <string>
// include most common headers
using namespace std;
One.h
#pragma once
#include "Core.h"
// if you need a header only for this class, put it here
// if you need a header in mutiple classes, put in Core.h
namespace one {
class One
{
public:
One();
~One();
void sumFromFirstNamespace(string firsNumber, string secondNumber)
{
//convert string to int
int first = stoi(firsNumber);
int second = stoi(secondNumber);
int result = first + second;
cout << result << endl;
}
};
}
Two.h
#pragma once
#include "Core.h"
// if you need a header only for this class, put it here
// if you need a header in mutiple classes, put in Core.h
namespace two{
class Two
{
public:
Two();
~Two();
void sumFromSecondtNamespace(string firsNumber, string secondNumber)
{
//convert string to int
int first = stoi(firsNumber);
int second = stoi(secondNumber);
int result = first + second;
cout << result << endl;
}
};
}
main.cpp
#include "One.h"
#include "Two.h"
int main()
{
one::One firstClass;
two::Two secondClass;
firstClass.sumFromFirstNamespace("10", "20");
secondClass.sumFromSecondtNamespace("20", "30");
}
There can be cases where you need the same 10+ headers in two different classes, i think that puting them in one header helps you see the code better.
And yes, preprocesor defines are also good, don't forget that. (:
I'm working on making a game in C++. I have declared a Constant namespace used for global values that I need access to throughout the program. In there I have an ofstream for debugging purposes (yeah, I know it's not "constant" but it fits best there), which outputs only when it feels like it. I was able to make a small program demonstrating the problem. I apologize for it being spread across 4 files, but it is important, I promise.
main.cpp:
// Include necessary files
#include "test.h"
#include "constants.h"
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
// Start of program
Constant::outstream.open("test.txt");
// ...
// Do stuff
// Output debugging info
Test test;
test.print("Test", Constant::outstream);
// ...
// Do other stuff
// End of program
Constant::outstream.close();
return 0;
}
constants.h:
#ifndef _CONSTANTS_H
#define _CONSTANTS_H
#include <fstream>
namespace Constant
{
static ofstream outstream;
}
#endif
test.h:
#ifndef _TEST_H
#define _TEST_H
#include <string>
#include <fstream>
#include "constants.h"
class Test
{
public:
void print(string str, ofstream& out);
};
#endif
test.cpp:
#include "test.h"
using namespace std;
void Test::print(string str, ofstream& out)
{
out << "out: " << str << endl << flush; // Works
Constant::outstream << "Constant::outstream: " << str << endl << flush; // Doesn't
}
In the test.cpp file, the out << ... line works as it should, while the Constant::outsream << ... line doesn't do anything even though I'm passing Constant::outstream as the out parameter! I don't see any reason why these two lines should be in any way different.
Before posting this, I tried putting test.cpp's code in test.h, just to have less files for the question, and was amazed to see it work. If I copy-paste the Test::print() function into test.h (whether inside or out of the class Test { ... }), then both output commands work correctly. the problem only occurs if Test::print()'s implementation is in a separate file.
It seems like any references to Constant::outstream simply don't work in class cpp files (no compile error, just nothing happens). It works in main.cpp and in class header files, but any class cpp file it seems not to. Unfortunately, this is a big program I'm writing so pretty much every class has its own cpp implementation file, and that's really the one place I need to use this ofstream. Does anyone know the reason for this?
Thanks in advance,
Doug
Constant::outstream has internal linkage, thus a separate instance is created for each translation unit. In short, Constant::outstream in test.cpp and main.cpp are two different variables.
§3.5.2 A name having namespace scope (3.3.6) has internal linkage if it is the name of
— a variable, function or function template that is explicitly declared static; or,
On the other hand, static class members would be visible throughout the program.
So, if you would write
struct Constant
{
static ofstream outstream;
}
instead of
namespace Constant
{
static ofstream outstream;
}
it would work.
However, note that the class must have external linkage; e.g. you should not put in in anonymous namespace.
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 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.
Encountered this problem before but forgot how I solved it.
I want to use the STL string class but the complier is complaining about not finding it.
Here is the complete .h file.
#ifndef MODEL_H
#define MODEL_H
#include "../shared/gltools.h" // OpenGL toolkit
#include <math.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Types.h"
class Model
{
public:
obj_type_ptr p_object;
char Load3DS (char *p_filename);
int LoadBitmap(char *filename);
int num_texture;
string fun("alex");
Model(char* modelName, char* textureFileName);
};
#endif
You want to be using std::string, yes?
You're just using string. Which works if you have a using namespace ... declaration, but isn't really a good idea in a header file.
Every identifier in the STL is in the std namespace. Until you do using namespace std;, using std::string;, or typedef std::string xxx;, it must be called std::string.
Any kind of using declaration in a header, especially outside your own namespace, is a bad idea, as others have mentioned.
So, import std::string into your class:
class Model
{
typedef std::string string;
public:
ohhh, std::string. Never use the using namespace in a header file btw.
As well as the namespace issue mentioned by other answers, you can't construct a variable at its declaration as a class member. Assuming you changed it to
class Model {
// ...
std::string fun("alex");
};
This is still illegal inside a class, you cannot assign a value in the declaration, you have to leave it:
class Model {
// ...
std::string fun;
};
And if you want to give it "alex" when it is created, initialise it in the constructor:
Model::Model(...)
: fun("alex") // initialiser
{
// ...
}