VC++ undeclared identifier - c++

Why this in Client.cpp file I'm getting error C2065:'TunnelContainer': undefined identifier?
Client.cpp code:
#include "stdafx.h"
#include "GClientLib.h"
using namespace GClientLib;
int _tmain(int argc, _TCHAR* argv[])
{
SettingsReader^ settings = gcnew SettingsReader();
SocketToObjectContainer^ STOContainer = gcnew SocketToObjectContainer();
TunnelContainer^ tunnels = gcnew TunnelContainer();
timeval time;
time.tv_sec = 0;
time.tv_usec = 300000;
....
GClientLib.h code fragment:
#include "Structures.h"
#include "Globals.h"
#include "SettingsReader.h"
#include "SocketToObjectContainer.h"
#include "SocketToSocketContainer.h"
#include "TunnelContainer.h"
Updated. SocketToSocketContainer.h
#ifndef SocketToSocketContainer_H
#define SocketToSocketContainer_H
#include <cliext/utility>
#include <cliext/list>
#include <cliext/algorithm>
namespace GClientLib {
ref class SocketToSocketContainer {
private:
cliext::list<cliext::pair<int, int>> sarasas;
public:
SocketToSocketContainer(void);
void Add(int, int);
int Find(int);
void Delete(int);
};
};
#endif
GclientLib is lib project, used in Client application. Build on Visual Studio 2013 C++/CLI enabled
TunnelContainer.h code:
#ifndef GClientLib_H
#define GClientLib_H
#include <cliext/utility>
#include <cliext/list>
#include <cliext/algorithm>
namespace GClientLib {
enum TunnelStatus
{
JUNGIASI = 1, //Uzmezgamas rysys tarp klientu
LAUKIA_PROGRAMOS = 2, // Laukia kol prisijungs norima kliento porgramine iranga
KOMUNIKACIJA = 3 // Tuneliu vyksta komunikacija
};
ref struct Tunnel
{
int tag; //Tunelio zyme
int dport; //Prievadas, prie kurio jungesi
int clientid; //Kliento ID su kuriuo sujungta
int sport; //Vietinis prievadas
int serverSocket; //Socketas, prie kuris priima duomenu srauta
int status; // Sujungimo statusas (Jungiasi, prisjungta, laukia jungties)
};
ref class TunnelContainer {
private:
// Tuneliu sarasas
cliext::list<cliext::pair<int, Tunnel^>> sarasas;
public:
// Konstruktorius
TunnelContainer();
// Pridedamas naujas tunelis. PERRASO statusa i JUNGIAMASI
Tunnel^ Add(Tunnel^ tunelis);
// Pridedamas naujas tunelis. Statusa nustato i JUNGIAMASI
Tunnel^ Add(int tag, int dport, int clientid, int sport, int serverSocket);
// Tunelio paieska pagal tag
Tunnel^ Find(int tag);
// Salina tuneli pagal tag
Tunnel^ Remove(int tag);
// Keicia tunelio statusa
void ChangeStatus(int tag, TunnelStatus status);
};
};
#endif
UPDATE
After moving TunnelContainer.h into first position getting this error in ToServerSocket.h file:
Error 1 error C2143: syntax error : missing ';' before '^' line 16
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int line 16
ToServerSocket.h code:
#ifndef ToServerSocket_H
#define ToServerSocket_H
#include <iostream>
#include "gNetSocket.h"
#include "ServerSocket.h"
#include "OutboundSocket.h"
namespace GClientLib {
ref class ToServerSocket : public gNetSocket {
private:
char *commandBuffer;
line 16 --->TagGenerator^ tag;
SocketToObjectContainer^ STOC;
SettingsReader^ settings;
public:
ToServerSocket(string ip, string port, fd_set* skaitomiSocket, fd_set* rasomiSocket, fd_set* klaidingiSocket, SocketToObjectContainer^ STOC, SettingsReader^ settings);
virtual int Send(char* data, int lenght) override;
virtual void Recive(SocketToObjectContainer^ container) override;
virtual void Connect() override;
virtual void Reconnect() override;
void CommandList(int page);
void CommandListAck(int rRecv);
void CommandHello();
void CommandHelp();
void CommandInitConnect(int id, int port, SocketToObjectContainer^ container);
void CommandConnect(SocketToObjectContainer^ container);
void CommandClear();
void CommandBeginRead(SocketToObjectContainer^ container);
void CommandClientConnectAck(SocketToObjectContainer^ container);
void CommandInitConnectAck();
void CommandJsonList(int page, SOCKET socket);
void CommandJsonListAck(int rRecv, SocketToObjectContainer^ container);
void CommandJsonInitConnect(int id, int port, SOCKET socket);
void CommandJSONConnect(SocketToObjectContainer^ container);
void CommandJsonInitConnectAck();
int GenerateTag();
};
};
#endif

you probably have a syntax error or missing ; or missing closing " in the include before that, "SocketToSocketContainer.h".
#include is a precompiler statement, all it does is include the content of the given file into the main file; it is your task to make sure that the result is a valid code. So if one include has an incomplete statement, the content of the second include continues that statement.
Edit: It could be even further up in the list - any open or incorrect #IFDEF in any include could remove the whole rest

I have found my mistake. GClientLib_H was already defined.
#ifndef GClientLib_H
#define GClientLib_H
Changed to
#ifndef TunnelContainer_H
#define TunnelContainer_H
Now everything is working. Thank you for quick responses

Related

C++ Class inheritance in different files

I'm trying to learn Inheritance mechanism in C++, I have made a Bancnote(Bills) class, and I want to make a class Card inheriting all the functions and variables from Class Bancnote.
And I get this type of error :
include\Card.h|6|error: expected class-name before '{' token|
BANCNOTE.H
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include <iostream>
#include "Card.h"
using namespace std;
class Bancnote
{
public:
Bancnote();
Bancnote(string, int ,int ,int );
~Bancnote( );
int getsumacash( );
void setsumacash( int );
int getsumaplata( );
void setsumaplata( int );
int getrest( );
void setrest( int );
string getnume( );
void setnume( string );
void ToString();
protected:
private:
string nume;
int sumacash;
int rest;
static int sumaplata;
};
#endif // BANCNOTE_H
BANCNOTE.CPP
#include <iostream>
#include "Bancnote.h"
#include "Card.h"
using namespace std;
int Bancnote::sumaplata=0;
Bancnote::Bancnote(string _nume,int _sumacash,int _rest, int _sumaplata )
{
this->nume=_nume;
this->sumacash=_sumacash;
this->rest=_rest;
this->sumaplata=_sumaplata;
}
Bancnote::Bancnote()
{
this->nume="";
this->sumacash=0;
this->rest=0;
this->sumaplata=0;
}
Bancnote::~Bancnote()
{
cout<<"Obiectul"<<"->" <<this->nume<<"<-"<<"a fost sters cu succes";
}
string Bancnote::getnume()
{
return nume;
}
void Bancnote::setnume(string _nume)
{
this->nume=_nume;
}
int Bancnote::getsumacash()
{
return sumacash;
}
void Bancnote::setsumacash(int _sumacash)
{
this->sumacash=_sumacash;
}
int Bancnote::getsumaplata()
{
return sumaplata;
}
void Bancnote::setsumaplata(int _sumaplata)
{
this->sumaplata=_sumaplata;
}
int Bancnote::getrest()
{
return rest;
}
void Bancnote::setrest(int _rest)
{
this->rest=_rest;
}
void Bancnote::ToString()
{
cout<< "-----"<<getnume()<< "-----"<<endl;
cout<<"Suma Cash: "<<this->getsumacash()<<endl;
cout<<"Suma spre plata: "<<this->getsumaplata()<<endl;
cout<<"Restul:"<<this->getrest()<<endl;
}
CARD.H
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
class Card: public Bancnote
{
public:
Card();
virtual ~Card();
protected:
private:
};
#endif // CARD_H
You have messed up the includes. What you have is more or less this:
Bancnote.h:
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include "Card.h" // remove this
struct Bancnote {};
#endif
Card.h
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
struct Card : Bancnote {}; // Bancnote is not yet declared
// when compiler reaches here
#endif
When in main you include Bancnote.h then this header includes Card.h so you try to declare Card before Bancnote is declared. Actually Bancnote does not need the definition of Card, so simply removing the include should fix it.
PS: there are other issues (see comments below your question). Most importantly it is not clear why a Card is a Bancnote. Second, never put a using namespace std; inside a header! (see here why)

Debugging C++ compiler error

I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ā€˜)ā€™ before ā€˜nā€™
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.

C++ compile error C2146 and C4430

Compiler show me 2 errors:
C2146 - syntax error: missing ';' before identifier 'begin'
C4430 - missing type specifier - int assumed. Note: C++ does not support default-int
Errors occur in my "Globals.h" file, at line 5.
It's here:
#pragma once
#include "Load.h"
#include "SortStream.h"
extern Load begin;
extern int sort_by;
extern int sort_type;
extern vector<Flight> all_flights;
extern SortStream sort_stream;
My "Globals.cpp" file looks like:
#include "Globals.h"
Load Begin;
int sort_by;
int sort_type;
vector<Flight> all_flights;
SortStream sort_stream;
And ofc, here is "Load.h" file:
#pragma once
#include <vector>
#include "Flight.h"
#include "Globals.h"
using namespace std;
class Load {
std::string path;
vector<Flight> allFlights;
public:
Load();
std::string get_path();
vector<Flight> get_allFlights();
void set_path(std::string p);
void set_allFlights(vector<Flight> v);
void load_flights();
};
And "SortStream.h" file:
#pragma once
#include <vector>
#include "Flight.h"
using namespace std;
class SortStream {
vector< vector < Flight > > layout;
int switchNum;
int iterationNum;
int compareNum;
public:
SortStream();
vector< vector < Flight > > get_layout();
int get_switchNum();
int get_iterationNum();
int get_compareNum();
void set_layout(vector< vector < Flight > > v);
void set_switchNum(int n);
void set_iterationNum(int n);
void set_compareNum(int n);
};
Does anyone knows reason? Tnx in advance
To see the fail, you have to understand how #includes work. Each #includeis replaced by a copy of the included file.
Looking at Load.h
#pragma once
#include <vector> <-- paste vector in here
#include "Flight.h" <-- paste Flight.h in here
#include "Globals.h" <-- paste Globals.h in here
using namespace std;
class Load {
std::string path;
vector<Flight> allFlights;
public:
Load();
std::string get_path();
vector<Flight> get_allFlights();
void set_path(std::string p);
void set_allFlights(vector<Flight> v);
void load_flights();
};
Let's paste in Globals.h and watch what happens:
#pragma once
#include <vector> <-- paste vector in here
#include "Flight.h" <-- paste Flight.h in here
//Globals.h begins here
#pragma once
#include "Load.h" <-- would paste Load.h in here, but it's protected by #pragma once
and will not be pasted in again
#include "SortStream.h" <-- paste SortStream.h in here
extern Load begin;
extern int sort_by;
extern int sort_type;
extern vector<Flight> all_flights;
extern SortStream sort_stream;
// end of Globals.h
using namespace std; <-- off topic: Very dangerous thing to do in a header
class Load {
std::string path;
vector<Flight> allFlights;
public:
Load();
std::string get_path();
vector<Flight> get_allFlights();
void set_path(std::string p);
void set_allFlights(vector<Flight> v);
void load_flights();
};
In this case we can see that Load begin; is referenced before Load is defined. Boom.
Circular references are almost always bad and in this case, it is lethal. In other words, Tarik Neaj for the win.
Load.h has no need for anything defined in Globals.h, so remove the include to break the cycle.

Expected class name before { token

Here are the two header files.Child and parent.Can anyone explain why this error occures?
Thanks in advance
SampleApplication.h:
#ifndef SAMPLEAPPLICATION_H_
#define SAMPLEAPPLICATION_H_
#include "ns3/CcnModule.h"
#include "ns3/CCN_Name.h"
#include <string>
class CcnModule;
class SampleApplication : public ns3::Application
{
public:
ns3::Ptr<CcnModule> ccnm;
static ns3::TypeId GetTypeId(void);
virtual ns3::TypeId GetInstanceTypeId (void) const;
SampleApplication();
SampleApplication(ns3::Ptr<CcnModule> ccnm);
virtual ~SampleApplication();
char* data;
int length;
ns3::Ptr<CCN_Name> dataName;
void SendInterest(ns3::Ptr<CCN_Name> n);
void SendData(ns3::Ptr<CCN_Name> data, char* buff, int bufflen);
virtual void InterestReceived(ns3::Ptr<CCN_Name> ccnn);
virtual void DataArrived(ns3::Ptr<CCN_Name> data, char* buff, int bufflen);
void AnnounceName(ns3::Ptr<CCN_Name> n);
virtual void DoDispose();
virtual void DoInitialize();
};
#endif
Receiver.h:
#ifndef RECEIVER_H_
#define RECEIVER_H_
#include "ns3/CcnModule.h"
#include "ns3/CCN_Name.h"
#include <string>
#include <vector>
#include "ns3/SampleApplication.h"
class CcnModule;
//class SampleApplication;
class Receiver : SampleApplication
{
public:
static ns3::TypeId GetTypeId(void);
virtual ns3::TypeId GetInstanceTypeId (void) const;
Receiver(ns3::Ptr<CcnModule> ccnm);
virtual ~Receiver();
void SendInterest(ns3::Ptr<CCN_Name> n);
virtual void DataArrived(ns3::Ptr<CCN_Name> data, char* buff, int bufflen);
};
#endif
I am not familiar with library you are using but I am guessing it is the NS-3 Discrete Event Network Simulator and from a cursory glance at the API I would expect to see the include
#include "ns3/application.h"
In your SampleApplication.h file.
It may well be that it is already pulled in by some of those other includes in your implementation but at first glance it appears to be missing.

How to correctly extend a class in C++ and writing its header file?

I've got a third party library named person.lib and its header person.h. This is my actual project structure and it compiles and runs perfectly.
Actual Structure:
main.cpp
#include <iostream>
#include <time.h>
#include <ctype.h>
#include <string>
#include "person.h"
using namespace person;
using namespace std;
class Client : public Person
{
public:
Client();
void onMessage(const char * const);
private:
void gen_random(char*, const int);
};
Client::Client() {
char str[11];
gen_random(str, 10);
this->setName(str);
}
void Client::onMessage(const char * const message) throw(Exception &)
{
cout << message << endl;
}
void Client::gen_random(char *s, const int len) {
//THIS FUNCTION GENERATES A RANDOM NAME WITH SPECIFIED LENGTH FOR THE CLIENT
}
int main()
{
try
{
Person *p = new Client;
p->sayHello();
}
catch(Exception &e)
{
cout << e.what() << endl;
return 1;
}
return 0;
}
I want to refactor my code by dividing the declaration of my Client class from its definition and create client.h and client.cpp. PAY ATTENTION: sayHello() and onMessage(const * char const) are functions of the person library.
Refactored Structure:
main.cpp
#include <iostream>
#include "client.h"
using namespace person;
using namespace std;
int main()
{
try
{
Person *p = new Client;
p->sayHello();
}
catch(Exception &e)
{
cout << e.what() << endl;
return 1;
}
return 0;
}
client.cpp
#include "client.h"
using namespace person;
using namespace std;
Client::Client() {
char str[11];
gen_random(str, 10);
this->setName(str);
}
void Client::onMessage(const char * const message) throw(Exception &)
{
cout << message << endl;
}
void Client::gen_random(char *s, const int len) {
//THIS FUNCTION GENERATES A RANDOM NAME WITH SPECIFIED LENGTH FOR THE CLIENT
}
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <time.h>
#include <ctype.h>
#include <string>
#include "person.h"
class Client : public Person
{
public:
Client();
void onMessage(const char * const);
private:
void gen_random(char*, const int);
};
#endif
As you can see, I've simply created a client.h in which there's the inclusion of the base class person.h, then I've created client.cpp in which there's the inclusion of client.h and the definitions of its functions. Now, the compilation gives me these errors:
error C2504: 'Person': base class undefined client.h 7 1 Test
error C2440: 'inizialization': unable to convert from 'Client *' to 'person::impl::Person *' main.cpp 15 1 Test
error C2504: 'Person': base class undefined client.h 7 1 Test
error C2039: 'setName': is not a member of 'Client' client.cpp 8 1 Test
error C3861: 'sendMessage': identifier not found client.cpp 34 1 Test
It's a merely cut&copy refactoring but it doesn't work and I really don't understand WHY! What's the solution and why it gives me these errors? Is there something about C++ structure that I'm missing?
Here's a dog-n-bird implementation (ruff ruff, cheep cheep)
cLawyer is defined and implemented in main.cpp, while cPerson and cClient are defined in their own header files, implemented in their own cpp file.
A better approach would store the name of the class. Then, one wouldn't need to overload the speak method - one could simply set the className in each derived copy. But that would have provided in my estimates, a less useful example for you.
main.cpp
#include <cstdio>
#include "cClient.h"
class cLawyer : public cPerson
{
public:
cLawyer() : cPerson() {}
~cLawyer() {}
void talk(char *sayWhat){printf("cLawyer says: '%s'\n", sayWhat);}
};
int main()
{
cPerson newPerson;
cClient newClient;
cLawyer newLawyer;
newPerson.talk("Hello world!");
newClient.talk("Hello world!");
newLawyer.talk("Hello $$$");
return 0;
}
cPerson.h
#ifndef cPerson_h_
#define cPerson_h_
class cPerson
{
public:
cPerson();
virtual ~cPerson();
virtual void talk(char *sayWhat);
protected:
private:
};
#endif // cPerson_h_
cPerson.cpp
#include "cPerson.h"
#include <cstdio>
cPerson::cPerson()
{
//ctor
}
cPerson::~cPerson()
{
//dtor
}
void cPerson::talk(char *sayWhat)
{
printf("cPerson says: '%s'\n",sayWhat);
}
cClient.h
#ifndef cClient_h_
#define cClient_h_
#include "cPerson.h"
class cClient : public cPerson
{
public:
cClient();
virtual ~cClient();
void talk(char *sayWhat);
protected:
private:
};
#endif // cClient_h_
cClient.cpp
#include "cClient.h"
#include <cstdio>
cClient::cClient()
{
//ctor
}
cClient::~cClient()
{
//dtor
}
Output
cPerson says: 'Hello world!'
cClient says: 'Hello world!'
cLawyer says: 'Hello $$$'
Suggestions noted above:
//In the cPerson class, a var
char *m_className;
//In the cPerson::cPerson constructer, set the var
m_className = "cPerson";
//Re-jig the cPerson::speak method
void cPerson::speak(char *sayWhat)
{
printf("%s says: '%s'\n", m_className, sayWhat);
}
// EDIT: *** remove the speak methods from the cClient and cLawyer classes ***
//Initialize the clas name apporpriately in derived classes
//cClient::cClient
m_className = "cClient";
//Initialize the clas name apporpriately in derived classes
//cLaywer::cLaywer
m_className = "cLawyer";
You are declaring the class Client twice - once in the .h file and once in .cpp. You only need to declare it in the .h file.
You also need to put the using namespace person; to the .h file.
If class Person is in namcespace person, use the person::Person to access it.
The client.cpp must contain definitions only!
I think for the linker the class Client defined in client.h and class Client defined in client.cpp are different classes, thus it cannot find the implementation of Client::Client(). I purpose to remove the declaration of class Client from the client.cpp and leave there only definitions of functions:
// client.cpp
#include <time.h>
#include <ctype.h>
#include <string>
#include "client.h"
using namespace std;
Client::Client()
{
//DO STUFF
}
void Client::onMessage(const char * const message)
{
//DO STUFF
}
void Client::gen_random(char *s, const int len) {
//DO STUFF
}