Failed to use namespace correctly in my .cpp file - c++

I declare a namespace MYTIME in mytime.h file.
#include<ostream>
#ifndef _TIME_H
#define _TIME_H
namespace MYTIME{
class time
{
...
};
}
#endif
And try to use it in my time.cpp file,
#include"mytime.h"
#include<iostream>
using namespace std;
using namespace MYTIME;
time::time()
{
years = days = hours = minutes = 0;
seconds = 0;
months = JAN;
}
when I use g++ time.cpp to compile, errors are as follows
time.cpp:8:17: error: ‘MYTIME’ is not a namespace-name
using namespace MYTIME;
^~~~~~
time.cpp:8:23: error: expected namespace-name before ‘;’ token
using namespace MYTIME;
^
time.cpp:10:1: error: ‘time’ does not name a type
time::time()
time.cpp:31:6: error: ‘time’ is not a class, namespace, or enumeration
void time::addSec(long int s)
time.cpp: In function ‘void addSec(long int)’:
time.cpp:34:7: error: ‘seconds’ was not declared in this scope
m = (seconds + s) / 60;
^~~~~~~
time.cpp:34:7: note: suggested alternative: ‘useconds_t’
m = (seconds + s) / 60;
^~~~~~~
useconds_t
... // many same errors
where am i going wrong? and how to fix it? Looking for help.

Your header guard is using a name that is already defined. As a result, your namespace and class is never declared.
A simple test demonstrates the problem:
#include <iostream>
int main()
{
#ifdef _TIME_H
std::cout << "Well, well, well...\n";
#endif
}
You would expect this program to have no output, but when compiled with g++ and run, this program outputs:
Well, well, well...
To solve this, you should use more uniquely named headers, and don't attempt to use naming conventions employed by the standard library. In particular, prefixing with an underscore is generally not a good idea.
If your compiler supports #pragma once you may wish to consider using that instead.

Related

BOOST_PHOENIX_ADAPT_FUNCTION causes invalid template error

I am trying to create a lazy function from a template function following the Boost::phoenix documentation. The code looks like this
#include <iostream>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/phoenix/object.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
#include <boost/phoenix/core/argument.hpp>
using namespace boost;
using namespace boost::phoenix;
namespace demo
{
bool func(double a,double b)
{
return bool(a > b);
}
}
BOOST_PHOENIX_ADAPT_FUNCTION( bool , func , demo::func , 2)
int main(int argc,char **argv)
{
namespace pl = boost::phoenix::placeholders;
auto comperator = func(pl::arg1,pl::arg2);
std::cout<<comperator(1.2,12.4)<<std::endl;
std::cout<<comperator(0.5,0.1)<<std::endl;
}
This is virtually one of the examples from the BOOST documentation. Storing this file as mk_lazy1.cpp and try to compile gives
$ g++ -omk_lazy1 mk_lazy1.cpp
mk_lazy1.cpp:26:1: error: template argument 1 is invalid
mk_lazy1.cpp:26:1: error: expected identifier before ‘::’ token
mk_lazy1.cpp:26:1: error: expected initializer before ‘const’
mk_lazy1.cpp: In function ‘int main(int, char**)’:
mk_lazy1.cpp:31:10: error: ‘comperator’ does not name a type
mk_lazy1.cpp:32:35: error: ‘comperator’ was not declared in this scope
I use gcc-4.7 on a Debian testing system. An honestly I am a bit lost as I have absolutely no idea what is wrong here (as I said, this is virtually a word by word copy of one of the examples provided by the Boost documentation).
Does anyone have a good idea?
Remove using namespaces and all will work fine.
Or write using namespaces AFTER adapt macro and all will work fine too.
Or put macro into unnamed namespace.

Compile error:... was not declared in this scope

I'm a neophyte of C++ programmation. I have to implement a program witch calculates the pseudoinverse of a matrix. As the Eigen tutorial suggests, I have written a code like this:
#include <stdio.h>
#include <stdlib.h>
#include <Core>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/SVD>
#include <Eigen/Eigen>
using namespace Eigen;
using namespace std;
void pinv(MatrixXf& pinvmat)
{
ei_assert(m_isInitialized && "SVD is not initialized.");
double pinvtoler=1.e-6; // choose tolerance
SingularValuesType m_sigma_inv=m_sigma;
for ( long i=0; i<m_workMatrix.cols(); ++i) {
if ( m_sigma(i) > pinvtoler )
m_sigma_inv(i)=1.0/m_sigma(i);
else m_sigma_inv(i)=0;
}
pinvmat = (m_matV*m_sigma_inv.asDiagonal()*m_matU.transpose());
}
int main()
{
MatrixXf A(3,2);
A<<1,2,3,4,5,6;
pinv(A);
cout << "pinv =" << endl << A << endl;
return 0;
}
If I try to compile it I'll get the errors:
tut_eigen/pinv.cpp: In function ‘void pinv(Eigen::MatrixXf&)’:
tut_eigen/pinv.cpp:18:14: error: ‘m_isInitialized’ was not declared in this scope
tut_eigen/pinv.cpp:18:58: error: ‘ei_assert’ was not declared in this scope
tut_eigen/pinv.cpp:20:4: error: ‘SingularValuesType’ was not declared in this scope
tut_eigen/pinv.cpp:20:23: error: expected ‘;’ before ‘m_sigma_inv’
tut_eigen/pinv.cpp:21:22: error: ‘m_workMatrix’ was not declared in this scope
tut_eigen/pinv.cpp:22:19: error: ‘m_sigma’ was not declared in this scope
tut_eigen/pinv.cpp:23:19: error: ‘m_sigma_inv’ was not declared in this scope
tut_eigen/pinv.cpp:24:22: error: ‘m_sigma_inv’ was not declared in this scope
tut_eigen/pinv.cpp:26:15: error: ‘m_matV’ was not declared in this scope
tut_eigen/pinv.cpp:26:22: error: ‘m_sigma_inv’ was not declared in this scope
tut_eigen/pinv.cpp:26:47: error: ‘m_matU’ was not declared in this scope
Why?? They are not declared in SVD file?
I suspect you talk about this "tutorial" which isn't so much a tutorial but an FAQ assuming you know a bit about the library already (it would be helpful if you link to your sources of information, BTW).
What this says is that you can add the pinv() method to the SVD "from the outside". I assume they mean that you can derive from SVD and provide the pinv() method in your derived class. Just typing the function somewhere doesn't give the compiler the necessary context to determine where referenced names are located.

"Not declared in scope" C++ issue

I'm writing a simple class in C++ for a class (school, not code). I have a little C++ experience, but it's been a while so I'm relearning whatever I forgot and learning a lot of new syntax (I have much more experience in Java). Here is the code:
#include <iostream>
#include <string>
using namespace std;
class Project112
{
private:
string romanNumeral;
int decimalForm;
public:
Project112()
{
romanNumeral = "";
decimalForm = 0;
}
int getDecimal()
{
return decimalForm;
}
};
and here is the driver:
include cstdlib
include <iostream>
using namespace std;
int main()
{
Project112 x;
int value2 = x.getDecimal();
return 0;
}
This is part of a larger program, but I've simplified it down to this because this is the where the problem lies. Every time I try to run the program, I get the following errors:
main.cpp:10: error: 'Project112' was not declared in this scope
main.cpp:10: error: expected `;' before 'x'
main.cpp:14: error: 'x' was not declared in this scope
Can someone please explain the problem? Thanks in advance.
#include "Project112.h"
Add that above main. You forgot to include the header file. And:
using namespace std;
Don't do that in a header file. That imports the everything from the std namespace into the global namespace of any file which includes your header. Just fully qualify the type in a header, i.e., std::string, and I would avoid that in implementation files as well in a large project (though something like using std::string is ok IMO in an implementation file).

Namespaces not found by the compiler

I have several files, listed just below, wherein I define some namespaces and classes. Then in one of the files I am trying to incorporate the classes from the others. The issue is that the compiler seems to not be able to find my namespaces.
charon.cpp
chin.cpp
chout.cpp
main.cpp
My namespaces are charon, charon_in, and charon_out. The main problems come up on a particular file, charon.cpp, so here is that file and chin.cpp too.
The errors:
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/sys/charon.o.d -o build/Debug/GNU-Linux-x86/sys/charon.o sys/charon.cpp
sys/charon.cpp:6:17: error: ‘charon_in’ is not a namespace-name
sys/charon.cpp:6:26: error: expected namespace-name before ‘;’ token
sys/charon.cpp:7:17: error: ‘charon_out’ is not a namespace-name
sys/charon.cpp:7:27: error: expected namespace-name before ‘;’ token
sys/charon.cpp:15:5: error: ‘chout_’ does not name a type
sys/charon.cpp:16:5: error: ‘chin_’ does not name a type
sys/charon.cpp:31:39: error: ‘chin_’ has not been declared
sys/charon.cpp:31:55: error: ‘engine_input’ was not declared in this scope
sys/charon.cpp:32:40: error: ‘chout_’ has not been declared
sys/charon.cpp:32:57: error: ‘engine_output’ was not declared in this scope
charon.cpp
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
using namespace std;
using namespace charon_in;
using namespace charon_out;
namespace charon
{
class charon_
{
private:
chout_ engine_output;
chin_ engine_input;
boost::thread input_thread;
boost::thread output_thread;
void start_threads();
void stop_threads();
public:
//Methods
};
chin.cpp
#include <iostream>
#include <borland/conio.h>
#include <ncurses.h>
#include <boost/thread.hpp>
using namespace std;
using namespace charon;
using namespace charon_out;
namespace charon_in
{
class chin_
{
private:
bool pause;
charon_* engine;
inline iostream grab();
public:
//Methods
};
I triple checked everything. So I am pretty confident the syntax is correct, but obviously I'm missing some key concept otherwise the compiler wouldn't complain.
(I know putting classes in the cpp files isn't a standard thing to do, but I know it is possible to do it so that's why I chose to try.)
I can't see what mistakes I've made. Any help would be appreciated.
You need to include the header file that declares the namespace (or declare it again) before the using directive:
// using namespace test; // Error test is not known to be a namespace
namespace test {}
using namespace test; // Fine -- test is known
As Wayne correctly points out, you probably want to restructure your code differently, including header files that will contain the declarations and can be included in different translation units.
You have the class declarations and the definitons in the .cpp files. You need to move the class declarations to a .h file and include it in the appropriate files that are using the class.
For example, move the following to chin.h and include chin.h in charon.cpp.
namespace charon_in
{
class chin_
{
private:
bool pause;
iostream key_sequence;
deque<char> key_queue;
charon_* engine;
inline iostream grab();
public:
chin_(const charon_& handle);
chin_(const chin_& orig);
~chin_();
void refresh();
bool stop_check();
};
}
Be wary of cyclic dependencies for they can also cause the compiler to not find the namespace you require.
//In rightWing.h
include "leftWing.h"
// code
//In leftWing.h
include "rightWing.h"
// code

"nice" keyword in c++?

So I was doing some simple C++ exercises and I noticed an interesting feat. Boiled down to bare metal one could try out compiling the following code:
class nice
{
public:
nice() {}
};
int main()
{
nice n;
return 0;
};
The result is a compilation error that goes something like this:
<file>.cpp: In function ‘int main()’:
<file>.cpp:11: error: expected `;' before ‘n’
<file>.cpp:11: warning: statement is a reference, not call, to function ‘nice’
<file>.cpp:11: warning: statement has no effect
And this was using regular g++ on Max OS X, some of my friends have tried in on Ubuntu as well, yielding the same result.
The feat seems to lie in the word "nice", because refactoring it allows us to compile. Now, I can't find the "nice" in the keyword listings for C++ or C, so I was wondering if anyone here had an idea?
Also, putting
class nice n;
instead of
nice n;
fixes the problem.
P.S. I'm a relative C++ newbie, and come from the ActionScript/.NET/Java/Python world.
Update:
Right, my bad, I also had an
#include <iostream>
at the top, which seems to be the root of the problem, because without it everything works just fine.
Maybe the problem is somehow caused by function nice in libc. It is similar to trying to name your class printf.
using namespace std, by any chance?
Edit:
The standard says that standard headers define all their symbols in namespace std (see 17.4.1.2.4).
A footnote, however, also says that the <.h> variants dump their names into the global namespace - but of course no one should be using these ;)
It is a namespace problem but not with namespace std. The header <iostream> is pulling in <unistd.h>
If you try
class nice
{
public:
nice() {};
};
int main(int argc, char *argv[])
{
nice n;
return 0;
}
there is no problem.
Simply add
#include <unistd.h>
and you will get the "expected ‘;’ before ‘n’" error. Namespace std does not enter the picture.
So the solution is the same as before - put class nice in its own namespace and it will not clash with the global ::nice().
Try this version:
#include <iostream>
namespace test
{
class nice
{
public:
nice() {}
};
};
using namespace std;
int main()
{
test::nice n;
cout << "well I think this works." << endl;
return 0;
}
In this case I've defined my own namespace test. Doing so, I can use whatever class names I like, including functions already defined like printf. The only things I can't re-use are reserved words like int or namespace.
Note: if you say:
using namespace test;
As well and refer to nice alone, you'll get this error:
nice.cpp: In function ‘int main()’:
nice.cpp:18: error: reference to ‘nice’ is ambiguous
/usr/include/unistd.h:593: error: candidates are: int nice(int)
nice.cpp:7: error: class test::nice
Which I think explains nicely what's going on - nice now exists in two namespaces and the compiler can't work out which one you mean.
It works fine for me. Did you try the exact code you posted?
extern "C"
{
#include <unistd.h>
}