Adding namespace functions in header files [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 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.

Related

C++ use of undeclared identifier [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 years ago.
Improve this question
I know there's a lot of different threads with this similar issue but I've gone through a number of them and have fixed any of the possible issues that may be resulting in this error but I keep getting it.
I'm trying to test some of functions in my class file but whenever I try calling them I get that error.
Here's one for example.
main.cpp
#include <iostream>
#include <vector>
#include "Graph.h"
using namespace std;
int main(){
Graph graph;
newState();
return 0;
}
Graph.h (I know I shouldn't be using namespace here but I'm still doing it for now)
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class Graph{
public:
Graph();
Graph(vector<Edge> const &edges, int N);
void printGraph(Graph const &graph, int N);
// print adjacency list representation of graph
void createTree(GraphNode *root, int state[]);
void newState();
private:
int states = 8;
int initialState[8];
int nextState[8];
};
Graph.cpp
#include "Graph.h"
void Graph::newState(){
srand (time(NULL));
int random = rand() % 4 + 1;
cout << random;
};
I feel like I'm trying to do the bare minimum but it just does not want to work. Can anyone please tell me where I'm going wrong?
newState is not a free-standing function. It is a non-static member function of Graph class. Which means, it must be called on an instance of Graph. What you probably meant to do is:
Graph graph;
graph.newState();

Random consts on initialization lists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
There seems to be an issue when I try to make initialization list with a const number while its a random number, cant realy figure out the problem and didnt found a proper solution when I use random const's on initizilation lists.
die.h
#ifndef die
#define die
#include <iostream>
#include "time.h"
#include "stdlib.h"
class die{
private:
const int dieFaces;
public:
die();
// Getters
int getFaces();
// Common Functions
void printDie(die);
void roll();
int copyConstructor(die);
// Destructors
~die(){};
};
#endif die
die.cpp
#include "die.h"
#include <iostream>
#include "time.h"
#include "stdlib.h"
#include <random>
using namespace std;
// Constructor
die::die() : dieFaces(rand() % 20 + 1){};
the error i'm getting is "Declartion does not declare anything" while on the task I was asked to create an empty constructor.
and in the CPP file it seems to expect all sorts of ";" and "Declartion of Variable expected"...
Any help will be appreciated. thanks.
Don't use the include guard die: it's the same as the class name.
The preprocessor will substitute empty text every time it sees the string die. The compiler will see
class {
private:
etc., which is not compilable.
Use something like #define included_die_hpp instead.

Having trouble with the error: expected unqualfied-id before '{' just a header for a class that I need just not sure what is causing the error [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
#include <iostream>
using namespace std;
{
class book
{
public:
set_book();
show_book();
private:
string title,author;
int pages,date;
};
}
In addition to the points made by other answers...
You have:
set_book();
show_book();
These are not valid member function declarations. They need, at the least, a return type.
void set_book();
void show_book();
Please add any input arguments needed by them.
You are missing includes (string), you should avoid using namespace std in a header.
And also, remove this extra scope :
using namespace std;
// { <------
class book
{
public:
set_book();
show_book();
private:
string title,author;
int pages,date;
};
// } <------
You have a bogus set of { and }.
Change to:
#include <iostream>
using namespace std; // this is a bad idea BTW
class book
{
// etc.
Also it'd be a good idea to #include <string> in case whoever includes your file does not include that; and if you don't actually use iostream then don't include it.

C++ Headers files and class definitions issue [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
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

VS2012 LibRTMP include c into 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 have Librtmp.dll in the debug folder where my exe is generated. The header and auxillary code files are available in my project and included as shown below.
Using this include.... i can use librtmp with intellisense.
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavdevice/avdevice.h"
#include "libavfilter/avfilter.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "librtmp/rtmp.h"
}
Here is the code example that is being used.
RTMP *r;
char uri[]="rtmp://localhost:1935/live/desktop";
r = RTMP_Alloc();
RTMP_Init(r);
RTMP_SetupURL(r, (char*)uri);
RTMP_EnableWrite(r);
RTMP_Connect(r, NULL);
RTMP_ConnectStream(r,0);
VS2012
IntelliSense: argument of type "RTMP *" is incompatible with parameter of type "RTMP *"
This occurs at this point first. Then once again, for each follow r variable.
r = RTMP_Alloc();
Some reading has suggested using a typedef.
Understanding typedefs for function pointers in C
This lead to...
typedef (RTMP*)(RTMP* rtmp);
However, Visual Studio just laughed at me... shaking it's head wondering if i even knew what i was doing.
IntelliSense: declaration of a member with the same name as its class
Any clues or ideas would be useful.
Thank you.
UPDATE - COMPLETE CODE
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavdevice/avdevice.h"
#include "libavfilter/avfilter.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
}
#include "librtmp/rtmp.h"
class RTMP
{
RTMP()
{
}
~RTMP()
{
}
typedef (RTMP*)(RTMP* rtmp);
void RTMP::Run()
{
//Code
//Init RTMP code
RTMP *r;
char uri[]="rtmp://localhost:1935/live/desktop";
r = RTMP_Alloc();
RTMP_Init(r);
RTMP_SetupURL(r, (char*)uri);
RTMP_EnableWrite(r);
RTMP_Connect(r, NULL);
RTMP_ConnectStream(r,0);
}
};
EPIC FACE PALM
Deepest Apologies
My class is called RTMP
Thank you #vard