C++ Headers files and class definitions issue [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Question: I have delcared a class, then used in my main function, it throws undefined error. Then I added .h file, and it throws another error. My question is about to write .h in accordance with the .cpp class file. Here are details:
I have this class declated in ElevatorButton.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class ElevatorButton
{
public:
int pressed = 0; // boolean
void myFunc(int param1)
{
cout << param1 << endl;
}
};
And then I have this file in ElevatorSimulation.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Header.h"
using namespace std;
int main()
{
ElevatorButton ElvBtn;
cout << ElvBtn.myFunc(1) << endl;
}
Compiling the code regularly throws error. I should declare header file as it seems. But in my header file, how should I manage the definition? I have read about .h file but still am confused. Here is my header.cpp:
#ifndef ElevatorButton_H
#define ElevatorButton_H
class ElevatorButton
{
public:
int pressed = 0; // boolean
public:
void myFunc(int param1){};
};
#endif
But it throws the following error with the Header.h:
1>ElevatorSimulation.cpp(14): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

You're getting that error because myFunc returns void, so
cout << ElvBtn.myFunc(1)
doesn't have a definition when the right hand side evaluates to void. If you want to print a value with cout, myFunc will have to return a type that can be passed into a stream.
The issue isn't with your class definition at all, it's in your main function.

Is it header.cpp? Because it should be header.h

Related

I get an error when I create my own header file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I will write the code of the header file and two .cpp files I created under the same folder below. And I tried this under Visual Studio, gcc-gnu, and dev c++. I am getting the following error in the file employe.cpp:
expected initializer before '.' token
employee.h:
#ifndef employee
#define employee
#include <iostream>
using namespace std;
class employe{
public:
string name;
int id;
int salary;
void showInfos();
};
#endif
employe.cpp:
#include "employee.h"
#include <iostream>
using namespace std;
void employe.showInfos(){
cout<<"Ad:"<<employe.name<<endl<<"Id:"<<employee.id<<endl<<"Salary:"<<employee.salary;
}
main.cpp:
#include <iostream>
#include "employee.h"
#include <iostream>
int main(){
employe.id=21;
cout<<employe.id;
return 0;
}
main.cpp errors:
expected unqualified-id before '.' token
expected primary-expression before '.' token
I meant to create my own header file and use it. But this happened.
employe.cpp fails because you are qualifying everything incorrectly. It needs to look more like this:
#include "employee.h"
#include <iostream>
using namespace std;
void employe::showInfos(){
cout << "Ad:" << name << endl << "Id:" << id << endl << "Salary:" << salary;
}
main.cpp fails because none of your struct members are static so you can't access any of them on the struct type itself, like you are trying to do. You need to declare an object instance of employe, eg:
#include "employee.h"
int main(){
employe emp;
emp.name = "Joe";
emp.id = 21;
emp.salary = 12345;
emp.showInfos();
return 0;
}
Online Demo

Adding namespace functions in header files [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to use header files to transfer functions in a namespace on a separate file to another file, and it's returning the error
"Add" is not a member of "simple".
I simply want to know if it's possible to use forward function declarations rather than declaring the function every time in the header, which supposedly would keep copying the code for every .cpp that includes it.
Here's the basic outline for 1.cpp, 2.cpp and 3.h.
1.cpp
#include "stdafx.h"
// Check this header file for function declarations.
#include "3.h"
int main()
{
//Example of a single namespace.
std::cout << simple::add(3, 4);
return 0;
}
2.cpp
#include "stdafx.h"
namespace simple {
int simple::add(int x, int y) {
return x + y;
}
}
3.h
#ifndef NAMESPACES
#define NAMESPACES
namespace simple {
int simple::add(int, int);
}
#endif
Much appreciation to anyone who answers, and apologies if this has been asked before.
Figured out the issue, for any future viewers of this thread.
You don't use the prepending "simple::" on header declarations, or in the function itself, as I did.
Corrected code:
3.h
#ifndef NAMESPACES
#define NAMESPACES
//Header guards are important!
namespace simple {
int add(int, int);
}
#endif
2.cpp
#include "stdafx.h"
#include "3.h"
int simple::add(int x, int y) {
return x + y;
}
No changes are required to 1.cpp.

In C++ why is it that int variable can be declared as private data member of a class, but not string variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
In C++ the following header file is legal
#ifndef SAMPLE_H_
#define SAMPLE_H_
class Sample {
private:
int number;
};
#endif
But the following header file is illegal
#ifndef
#define
class Sample {
private:
string name;
};
#endif
Why is it like that?
In my case I have the following header file:
Alphabet.h
#include <string>
#ifndef ALPHABET_H_
#define ALPHABET_H_
class Rhyme {
private:
string a;
public:
Rhyme ();
};
#endif
Alphabet.cpp
#include <iostream>
#include "Alphabet.h"
using namespace std;
Rhyme::Rhyme () {
a = "A for Apple";
}
Main.cpp
#include <iostream>
#include "Alphabet.h"
using namespace std;
int main () {
Rhyme rhyme;
return 0;
}
Linux terminal command:
g++ *.cpp
./a.out
After this I am getting the following error:
Error:
In file included from Alphabets.cpp:2:0:
Alphabet.h:10:2: error: ‘string’ does not name a type
string a;
^
Alphabets.cpp: In constructor ‘Rhyme::Rhyme()’:
Alphabets.cpp:8:2: error: ‘a’ was not declared in this scope
a = "A for Apple";
^
In file included from Main.cpp:2:0:
Alphabet.h:10:2: error: ‘string’ does not name a type
string a;
I am trying to declare a string member variable in header file as private, and then initialize it from another file using constructor
In C++, int is a builtin keyword and is a valid type anywhere in the code. string is a class in the std namespace defined in the <string> header and may only be used if you include the header first.
You should not use the using namespace directive in header files (namespace pollution), so you need to write std::string.
Also, use the file name of your header (e.g. SAMPLE_H) for include guard:
#ifndef SAMPLE_H
#define SAMPLE_H
#include <string>
class Sample {
private:
std::string name;
};
#endif
std::string is a standard user-defined class that is declared in the header <string>. So you need to include the header
#include <string>
and is placed in the standard name space std.
So you need at least write
class Sample {
private:
std::string name;
};

Compile error C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new in C++. When I compile this code compiler reports error-
main.cpp-
#include <iostream>
#include <string>
#include "main.h"
using namespace std;
string strings::getstr(string str)
{
return str;
}
int main()
{
strings strstr;
string constr;
string msg;
msg = "Hello World!";
constr = strstr.getstr(msg);
cout << constr;
return 0;
}
main.h-
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include <string>
class strings
{
public:
string getstr (string str);
};
#endif // MAIN_H_INCLUDED
error-
error: 'string' does not name a type
error: no 'std::string strings::getstr(std::string)' member function declared in class 'strings'
error: In function 'int main()':
error: 'class strings' has no member named 'getstr'
I am using Code::Blocks and gcc
I have written this simple code because I am working on a project and when I want to compile I always getting
'string' does not name a type
sorry for bad english...
The correct name for the string class is 'std::string' because it is declared within the 'std' namespace (the same goes for 'cout'). After you change 'string' into 'std::string' and use 'std::cout' instead of 'cout' your program will compile correctly.
Another way to do it is to place 'std' the as the first namespace:
#include <string>
using namespace std;
Personally, I don't like to use 'using namespace ...' (It's difficult for me to keep track of different namespaces ).
This could fix the problem
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include <string>
class strings
{
public:
std::string getstr(std::string str);
};
#endif // MAIN_H_INCLUDED
use using namespace std; after the header in main.h

main.cpp|45|error: expected primary-expression before "int" and "double" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
File sys.cpp:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <fstream>
using namespace std;
[...]
struct kmph_in_mps
{
int kmph[4];
int result[4];
void kmph_erfassen()
{
for (size_t i = 0; i < 4; ++i)
{
cin >> kmph[i];
}
}
void mps_erfassen(int kmph, double result)
{
result = kmph / 3.6;
}
void ergebniss_ausgeben()
{
cout << endl << kmph << "Km/h sind " <<result << " Meter pro Sekunde\n";
}
};
[...]
File main.cpp:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include "sys.cpp"
#include <fstream>
kmph_in_mps c;
[...]
void Kmph_in_mps()
{
system("cls");
cout << "\nKm/h: ";
c.kmph_erfassen();
c.mps_erfassen(int kmph, double result);
c.ergebniss_ausgeben();
t.beenden();
}
[...]
Errors:
Error: expected primary-expression before “int”
Error: expected primary-expression before “double”
I am a learning C++, and I dont get what to do now.
I am searching for answers at the internet and here, but I dont realy find the right one.
Which primary expression do I have to write before "in" and "double"?
Or am I doing everything completly wrong, like a beinner does? :P
Edits:
I tryed return result, but it seems to be not allowed in a void.
I already tryed c.mps_erfassen(); but it is gives me an error, too: error: no matching function for call to `kmph_in_mps::mps_erfassen()'|
I already tryed c.mps_erfassen(kmph, result); but then I do not declare them both in this scope. They are declared in the other file (sys.cpp). :S
c.mps_erfassen(int kmph, double result);
//^^^remove int and double
When you call a function, you should not put the type before the parameters.
This
c.mps_erfassen(int kmph, double result);
should be
c.mps_erfassen(kmph, result);
Let function deduce the type :)
EDITED IN RESPONSE TO COMMENT:-
You are creating object of struct in main.cpp while it's definition is in sys.cpp. How would main.cpp would get to know what your struct means.
For better design, place declaration of struct in header file say sys.h, then define required members in .cpp file say sys.cpp ( you have to include sys.h ). And then use that struct in your main.cpp ( again you have to include sys.h over here ).