Compile error:... was not declared in this scope - c++

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.

Related

Failed to use namespace correctly in my .cpp file

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.

How can I call a namespace inside a function?

Something interesting and wrong about my code.
#include <iostream>
void func(){
using namespace std;
}
main(){
func(); //Here the function will introduce the (using namespace std declaration) in the code
cout << "Hello World!";
return (0);
}
When compiled the error message is shown:
atizva#atizva:~/Documents/C++/Programs$ g++ -o func function_call.cpp
function_call.cpp: In function ‘int main()’:
function_call.cpp:7:2: error: ‘cout’ was not declared in this scope
cout << "Hello World!";
^~~~
function_call.cpp:7:2: note: suggested alternative:
In file included from function_call.cpp:1:0:
/usr/include/c++/7/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^~~~
I don't understand why the function 'func()' doesn't call the tag: 'using namespace std' appropriately.
To fix this, you would have to move using namespace std; outside of func(). The reason this fails in your current code is that using declaration only applies within the scope that it is called (in this case func()). So once you exit func(), you lose the effects of using namespace std;
You assume that namespaces are enabled at runtime, but namespaces are only meaningful at compile time. What you are doing is to limit the use of std to the scope of the function func(). That is, it allows you to type
cout
inside that function, but not elsewhere.

how to use digamma function in boost

I don't understand how the digamma function of boost can be used inside a program. Any example, is appreciated. I included boost
#include <boost/math/special_functions/digamma.hpp>
but the function call digamma(x), where x is a double gives the following error:
error: there are no arguments to ‘digamma’ that depend on a template
parameter, so a declaration of ‘digamma’ must be available
[-fpermissive]
Here's an example:
http://cpp.sh/7bdu
#include <boost/math/special_functions/digamma.hpp>
#include <iostream>
int main() {
std::cout << boost::math::digamma(3.14) << "\n";
}
edit: The question was edited with an error message. The error message means that the compiler didn't find a definition of digamma, because you didn't include the namespace bit boost::math::.

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.

"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).