g++ complaining about STD functions - c++

I have a section of downloaded source code, when trying to compile via Cygwin using g++ compiler, the compiler gives me an error saying that the 'transform' function is undeclared in this scope...
I am using the std namespace, and I have the correct headers. I am not sure why it is not compiling.. The syntax looks correct
Here is the code block section.
string tolower (const string & s)
{
string d = s;
transform(d.begin(), d.end(), d.begin(), (int(*)(int)) tolower);
return d;
} // end of tolower
Here is my header section:
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
// standard library includes ...
#include <string>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <sstream>
#include <ios>
#include <iterator>
using namespace std;

You need to include the appropriate header for std::transform:
#include <algorithm>
You should also avoid using namespace std; in the global namespace in headers, you pollute the global namespace of any code that includes your header file.
See this post about using namespace std.

Related

c++ Incomplete type error when defining AppServiceConnection with WinRT Console App

I am trying to create a AppServiceConnection but when doing so I get a Incomplete Type not allowed
I verified the header file is imported for that class as mention in other stackoverflow questions.
I tried several different attempted to define the AppServieConnection. Am I putting in the wrong place?
The only way it worked was putting it above the main method.
Here is my code
#include "pch.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <windows.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <tchar.h>
#include <stdio.h>
#include <ppltasks.h>
#include <appmodel.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <ppltasks.h>
#include <windows.h>
#include <appmodel.h>
#include <malloc.h>
#include <stdio.h>
#include <assert.h>
using namespace winrt;
using namespace concurrency;
using namespace Windows::Foundation;
using namespace std;
using namespace Windows::ApplicationModel::AppService;
AppServiceConnection connection_worked; // defining it here works but cannot call any methods from it
int main()
{
init_apartment();
Uri uri(L"http://aka.ms/cppwinrt");
printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
}
class NewClass {
private:
Windows::ApplicationModel::AppService::AppServiceConnection connection{nullptr};
Windows::ApplicationModel::AppService::AppServiceConnection connection2;
Windows::ApplicationModel::AppService::AppServiceConnection connection3 = nullptr;
AppServiceConnection connection4;
AppServiceConnection connection5 = nullptr;
AppServiceConnection connection6 = AppServiceConnection();
IAsyncAction InitializeAppServiceConnection() {
}
public:
NewClass() {
}
};
The error is clear, you are missing AppServiceConnection header file.
Please add the header file
#include <winrt/Windows.ApplicationModel.AppService.h>

Error: file not recognized: File format not recognized c++

My Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
int main() {
cout << "hi my name is me" << endl;
return 0;
}
I used the command:
:!g++ %
to compile my code
When it compiled it returned:
testing.cpp~: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
shell returned 1
Furthermore, this is my current bits/stdc++.h header file:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
How do you fix this issue?
Assuming you're running this within vim, you're doing the right thing, as % gets replaced by the current file.
However, the fact that it's trying to compile testing.cpp~ (the vim backup for the testing.cpp file) indicates to me that you're editing the wrong file (or putting a ~ after the % on your command line).

OpenCV HOGDescriptor undefined

I wanted to use HOG but when I tried cv::HOGDescriptor hog; it gives me it is undefined? I included:
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <vector>
#include <iterator>
#include <ctype.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <math.h>
#include <numeric>
#include <cstdio>
#include <stdlib.h>
#include <iostream>
cv::HOGDescriptor is defined in "objdetect.hpp" .you have to include this header

c++ unable to read memory with custom class when calling a function

I am having problems running functions within a custom class. Here is my code:
ROBOTSTRUCTURE.H
#pragma once
#include "math.h"
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
using namespace std::chrono;
class RobotStructure
{
bool faceTrackingEnabled;
public:
RobotStructure();
~RobotStructure();
bool testFunction(string input);
ROBOTSTRUCTURE.CPP
#include "RobotStructure.h"
RobotStructure::RobotStructure()
{
faceTrackingEnabled = true;
}
RobotStructure::~RobotStructure()
{
}
bool RobotStructure::testFunction(string input)
{
cout << input << endl; //THIS DOES NOT WORK, When using debugger shows that the entire class "Robot Structure" as unable to read memory
return true;
}
MAIN
#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <fstream>
#include "RobotStructure.h"
using namespace std;
int main()
{
RobotStructure *mybot= new RobotStructure();
mybot->testFunction("test");
return 0;
}
If a set a breakpoint in main, my class is initialized correctly and everything is fine. As soon as a call the "test function" the class falls out of memory within the test function.
When it goes back to main just before return 0; the class is also out of memory. As if the pointer is deleted somehow. Can someone please explain what is happening and what did I do that is wrong?
You declared the function as bool testFunction(string input) but the function is not returning anything. This leads to undefined behavior in C++.
In addition your example wouldn't compile (you declared an argument input but you are using intput).
Your testFunction does not use any class members. If you compile this code with optimization enabled ("Release"-Configuration in VisualStudio, -O2 on gcc), the compiler might render some variables (like this) "undebuggable".

Error: Expected constructor, destructor, type conversion before '<' token

the code is
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iterator>
#include <queue>
#include <algorithm>
#include <string>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <new>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
using std::vector;
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;
boost::numeric::ublas::matrix<double> A_MATRIX(A_MATRIX_ROWS,A_MATRIX_COLUMNS);
boost::numeric::ublas::matrix<double> Y_MATRIX(A_MATRIX_ROWS,1);
vector <double> GPSR_BB(boost::numeric::ublas::matrix<double> &f_Y_MATRIX,boost::numeric::ublas::matrix<double> &f_A_MATRIX,int f_tau,int f_tolA){
vector<double> objective(2);
//sth inside function
return objective;
}
int main(){
vector<double> objectives(maxiter+2);
objectives=GPSR_BB(Y_MATRIX,A_MATRIX,tau,tolA);
return 0;
}
in line
vector <double> GPSR_BB(boost::numeric::ublas::matrix<double> &f_Y_MATRIX,boost::numeric::ublas::matrix<double> &f_A_MATRIX,int f_tau,int f_tolA){
I receive error
error: expected constructor, destructor, or type conversion before ‘<’ token function
I guess, the problem is because of matrix data type, from boost library, which I have to pass to function, I don't think there is another way I can do for my specific problem.
Any help is greatly appreciated. Thank you
boost::numeric::ublas has vector as well as namespace std. Try removing using namespace std and using the appropriate namespace to refer to the correct type.
Try without a space between vector and <double>
vector<double> GPSR_BB