In my previous question, I asked that how can I extern classes in a class, and we could do:
namespace kc
{
using ::A;
using ::B;
}
instead of:
namespace kc
{
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
}
And now, I want to do something like this for namespaces, without defining the entire namespace again. I tried to use the same code, but it doesn't work: error: using-declaration may not name namespace <namespace_name>
Edit
Actually, I'm making a kernel, and that's my code (pio.hpp):
#pragma once
namespace pio
{
void outb(unsigned short port, unsigned char value);
void outw(unsigned short port, unsigned short value);
unsigned char inb(unsigned short port);
unsigned short inw(unsigned short port);
}
And (k.hpp):
#pragma once
#include <pio.hpp>
#include <cursor.hpp>
namespace k
{
using ::pio;
using ::cursor;
}
I think what you’re looking for is a namespace alias:
namespace A {
namespace B {
struct C {};
}
}
namespace X {
namespace Y = ::A::B;
}
This would allow you to write e.g.
X::Y::C foo;
Related
I declare a namespace in file test.h as follows:
namespace dev {
class test {
public:
test();
~test();
int m_number;
};
int sumInt(int, int);
}
When I complete file test.cpp as follows:
using namespace dev;
test::test() {
m_number = 1;
}
test::~test() {}
int sumInt(int a, int b) {
return a + b;
}
There occurs an error "Unresolved external symbol dev::sumInt(int,int)"
So I have to explicit introduce function sumInt to namespace dev like:
int dev::sumInt(int a, int b);
I'm very confused that when I using the namespace dev in file test.cpp,
why should method sumInt() be explicit defined in namespace dev,but class test should't be?
Sorry my English is poor,I have try my best to describe the question clearly.
I hope someone can enlighten me.
#include <iostream>
using namespace std;
void input(partsType inventory[100]&)
{}
int main()
{
struct partsType
{
string partName;
int partNum;
double price;
int quantitiesInStock;
};
partsType inventory[100];
}
I am trying to use a struct variable as a formal parameter. Later I will pass the variable by reference.
Currently, I am getting the error
declaration is incompatible, and `partsType` is undefined.
You have two issues:
You need to define the partsType outside the main, and of-course
before the input function otherwise, it does not know what is partsType.
Secondly, your function parameter syntax is wrong. It should have
been
void input(partsType (&inventory)[100])
// ^^^^^^^^^^^^^^^^^^ --> if you meant to pass the array by ref
So you need:
#include <iostream>
#include <string> // missing header
struct partsType
{
std::string partName;
int partNum;
double price;
int quantitiesInStock;
};
void input(partsType (&inventory)[100])
{}
int main()
{
partsType inventory[100];
}
Another option is to forward declare the struct partsType before the input function. But, that will need to make the function definition after the main, as you define the struct inside the main:
#include <iostream>
#include <string> // missing header
// forward declaration
struct partsType;
void input(partsType(&inventory)[100]);
int main()
{
struct partsType
{
std::string partName;
int partNum;
double price;
int quantitiesInStock;
};
partsType inventory[100];
}
void input(partsType(&inventory)[100])
{
// define
}
Also do not practice with using namespace std;
I am getting this error
error C2511: 'printlocation' : overloaded member function 'int (void)' not found in 'creature'
Here is the code:
Location.h
#ifndef location_h
#define location_h
class location
{
public:
setpoint(int,int,int);
getpoint(int,int,int);
private:
int x,y,z;
};
#endif
.cpp code below:
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include"location.h"
location::getpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
code for creature class :
#ifndef creature_h
#define creature_h
#include"location.h"
#include<string>
//using std::string;
using namespace std;
class creature
{
public:
creature();
moveto(location l);
getname(string n);
printlocation(string ,location );
private:
location lo;
string name;
};
#endif
code for creature.cpp in which there is an error:
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using namespace std;
#include<string>
#include"creature.h"
#include"location.h"
creature::creature()
{
lo;
name;
}
creature::getname(string n)
{
name=n;
cout<<"enter name";
cin>>n;;
}
creature::printlocation()
:name(n),lo(l)
{
name=n;
lo=l;
cout<<lo.setpoint(int,int,int);
}
-----still error exists....---
#ifndef creature_h
#define creature_h
#include"location.h"
#include<string>
//using std::string;
using namespace std;
class creature
{
public:
creature();
void moveto(location l);
void getname(string );
void printlocation(string ,location );
private:
location lo;
string name;
};
#endif
.cpp
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using namespace std;
#include<string>
#include"creature.h"
#include"location.h"
/*creature::creature()
{
lo;
name;
}*/
void creature::getname(string n)
{
name=n;
cout<<"enter name";
cin>>n;
}
void creature::printlocation()
:name(n),lo(l)
{
name=n;
lo=l;
cout<<l.getpoint(int,int,int);
}
main.cpp
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#include"location.h"
#include"creature.h"
#include<string>
main()
{
string n;
int x,y,z;
location l;
l.getpoint(x,y,z);
creature c;
c.getname(n);
c.printlocation(n,l);
return 0;
}
Declaration of function has to specify it's return type (and correct namespace for arguments, since I don't see using namespace std):
void printlocation( std::string ,location );
Add the same for other declarations:
void creature();
void moveto(location l);
void getname(string n);
Then call printlocation this way:
std::string s;
location l;
//... give some value to s and l ...
printlocation( s, l);
Also change its name to conform to cammel case: printLocation, is better.
All your member function declarations have no return type. For example
class location
{
public:
setpoint(int,int,int);
getpoint(int,int,int);
//...
Ot
class creature
{
public:
//...
moveto(location l);
getname(string n);
printlocation(string ,location );
//...
You have to specify return types of the functions.
For example
void setpoint(int,int,int);
Also function getpoint logically more corresponds to function setpoint because it sets values of data members of an object
location::getpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
So I would rename the function as setpoint. For example
void location::setpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
After you have fixed the declaration errors mentioned in the already existing answers, give all of your definitions in the .cpp files the same signature as used in the declaration:
// NOTE the parameter defintions!!
void creature::printlocation(string n,location l) {
name=n;
lo=l;
cout<<lo.setpoint(1,2,3);
}
Please also note that the member initializer syntax is only valid for usage in constructor functions:
void creature::printlocation() : name(n),lo(l)
// ^^^^^^^^^^^^^^^ This is wrong!!!
UPDATE:
You probably meant to provide a parametrized constructor function for creature
class creature {
public:
creature() (string n,location l) : name(n),lo(l) {}
// ...
};
I am trying to use forward declarations in header files to reduce the number of #include used and hence reduce dependencies when users include my header file.
However, I am unable to forward declare where namespaces are used. See example below.
File a.hpp:
#ifndef __A_HPP__
#define __A_HPP__
namespace ns1 {
class a {
public:
a(const char* const msg);
void talk() const;
private:
const char* const msg_;
};
}
#endif //__A_HPP__
File a.cpp:
#include <iostream>
#include "a.hpp"
using namespace ns1;
a::a(const char* const msg) : msg_(msg) {}
void a::talk() const {
std::cout << msg_ << std::endl;
}
File consumer.hpp:
#ifndef __CONSUMER_HPP__
#define __CONSUMER_HPP__
// How can I forward declare a class which uses a namespace
//doing this below results in error C2653: 'ns1' : is not a class or namespace name
// Works with no namespace or if I use using namespace ns1 in header file
// but I am trying to reduce any dependencies in this header file
class ns1::a;
class consumer
{
public:
consumer(const char* const text) : a_(text) {}
void chat() const;
private:
a& a_;
};
#endif // __CONSUMER_HPP__
Implementation file consumer.cpp:
#include "consumer.hpp"
#include "a.hpp"
consumer::consumer(const char* const text) : a_(text) {}
void consumer::chat() const {
a_.talk();
}
Test file main.cpp:
#include "consumer.hpp"
int main() {
consumer c("My message");
c.chat();
return 0;
}
UPDATE:
Here is my very contrived working code using the answer below.
File a.hpp:
#ifndef A_HPP__
#define A_HPP__
#include <string>
namespace ns1 {
class a {
public:
void set_message(const std::string& msg);
void talk() const;
private:
std::string msg_;
};
} //namespace
#endif //A_HPP__
File a.cpp:
#include <iostream>
#include "a.hpp"
void ns1::a::set_message(const std::string& msg) {
msg_ = msg;
}
void ns1::a::talk() const {
std::cout << msg_ << std::endl;
}
File consumer.hpp:
#ifndef CONSUMER_HPP__
#define CONSUMER_HPP__
namespace ns1
{
class a;
}
class consumer
{
public:
consumer(const char* text);
~consumer();
void chat() const;
private:
ns1::a* a_;
};
#endif // CONSUMER_HPP__
File consumer.cpp:
#include "a.hpp"
#include "consumer.hpp"
consumer::consumer(const char* text) {
a_ = new ns1::a;
a_->set_message(text);
}
consumer::~consumer() {
delete a_;
}
void consumer::chat() const {
a_->talk();
}
File main.cpp:
#include "consumer.hpp"
int main() {
consumer c("My message");
c.chat();
return 0;
}
To forward declare class type a in a namespace ns1:
namespace ns1
{
class a;
}
To forward declare a type in multiple level of namespaces:
namespace ns1
{
namespace ns2
{
//....
namespace nsN
{
class a;
}
//....
}
}
Your are using a a member of consumer which means it needs concrete type, your forward declaration won't work for this case.
For nested namespaces, since C++17, you can do
namespace ns1::ns2::nsN
{
class a;
}
Apart to forward-declare the class from within its namespace (as #billz says), remember to either use (prepend) that namespace when referring to the forward-declared class, or add a using clause:
// B.h
namespace Y { class A; } // full declaration of
// class A elsewhere
namespace X {
using Y::A; // <------------- [!]
class B {
A* a; // Y::A
};
}
Ref: Namespaces and Forward Class Declarations
I have a C++ program and when I try to compile it it gives an error:
calor.h|6|error: expected unqualified-id before ‘using’|
Here's the header file for the calor class:
#ifndef _CALOR_
#define _CALOR_
#include "gradiente.h"
using namespace std;
class Calor : public Gradiente
{
public:
Calor();
Calor(int a);
~Calor();
int getTemp();
int getMinTemp();
void setTemp(int a);
void setMinTemp(int a);
void mostraSensor();
};
#endif
Why does this happen?
This class inherits from gradiente:
#ifndef _GRADIENTE_
#define _GRADIENTE_
#include "sensor.h"
using namespace std;
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
}
#endif
Which in turn inherits from sensor
#ifndef _SENSOR_
#define _SENSOR_
#include <iostream>
#include <fstream>
#include <string>
#include "definicoes.h"
using namespace std;
class Sensor
{
protected:
int tipo;
int IDsensor;
bool estadoAlerta;
bool estadoActivo;
static int numSensores;
public:
Sensor(/*PARAMETROS*/);
Sensor(ifstream &);
~Sensor();
int getIDsensor();
bool getEstadoAlerta();
bool getEstadoActivo();
void setEstadoAlerta(int a);
void setEstadoActivo(int a);
virtual void guardaSensor(ofstream &);
virtual void mostraSensor();
// FUNÇÃO COMUM
/* virtual int funcaoComum() = 0;
virtual int funcaoComum(){return 0;};*/
};
#endif
For completeness' sake, here's definicoes.h
#ifndef _DEFINICOES_
#define _DEFINICOES_
const unsigned int SENSOR_MOVIMENTO = 0;
const unsigned int SENSOR_SOM = 1;
const unsigned int SENSOR_PRESSAO = 2;
const unsigned int SENSOR_CALOR = 3;
const unsigned int SENSOR_CONTACTO = 4;
const unsigned int MIN_MOVIMENTO = 10;
const unsigned int MIN_SOM = 10;
const unsigned int MIN_PRESSAO = 10;
const unsigned int MIN_CALOR = 35;
#endif
What am I doing wrong?
There is a semicolon missing at the end of this class:
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
} // <-- semicolon needed after the right curly brace.
Also, the names of your include guards are illegal. Names that begin with an underscore and an uppercase letter are reserved for the C++ implementation (as are names containing a double underscore) - you are not allowed to create such names in your own code. And you should never use:
using namespace std;
in a header file. And lastly, the destructor in your Sensor base class should almost certainly be made virtual.
In gradiente.h you forgot the semicolon at the end of your class declaration.
You need this:
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
};
See the added semicolon?
You forgot to leave the last semi colon on the closing brackets, };, on the gradiente class.