This program compiles just fine:
#include <iostream>
#include <stdio.h>
#include <boost/program_options.hpp>
int main(int argc, char* argv[]) {
std::cout << "Hello world" << std::endl;
}
This program gives a long compilation error:
#include <iostream>
namespace cio {
#include <stdio.h>
}
#include <boost/program_options.hpp>
int main(int argc, char* argv[]) {
std::cout << "Hello world" << std::endl;
}
The full dump of the compilation error: http://codepad.org/aIcQqkgH
The linux command I'm using to compile the program is: c++ -o main.cpp.o -c main.cpp
Simply use
#include <cstdio>
http://en.cppreference.com/w/cpp/header/cstdio
This header is officially required to declare all the legacy C library declarations inside namespace std.
However, if you have a "problem" that requires these "solutions", indeed just namespace your own stuff. If it's actually Boost that pollutes the global namespace, then file a bug.
Related
This is my main.cpp
#include <iostream>
#include <string>
#include "Tokenizer.h"
using namespace std;
int input;
int main(int argc, char const *argv[]){
Tokenizer obj("yeet");
cout << obj.getString();
cin >> input;
return 0;
}
This is my Tokenizer.h
#ifndef TOKENIZER_H
#define TOKENIZER_H
#include <string>
class Tokenizer{
public:
Tokenizer(std::string m);
std::string getString();
protected:
private:
std::string token;
};
#endif // TOKENIZER_H
This is my Tokenizer.cpp
#include "Tokenizer.h"
#include <string>
Tokenizer::Tokenizer(std::string m){
token=m;
//code
}
std::string Tokenizer::getString(){
return token;
}
when i compile with g++ it works fine and when i open a.exe i get this error.
The procedure entry point
_ZNSt7_cxx1112basic_stringlcSt11char_traitslcESalcEEC1EPKcRKS^_ could not be located in the dynamic link libary c:\"My project path"
(All files are in same folder.)
and i compiled with int without strings it worked fine i guess it is a error with #include <string>
In Mingw You have to explicitly specify libgcc libstdc++ the libraries. Use the following command
g++ Tokenizer.cpp main.cpp -o main -static-libgcc -static-libstdc++
#include <log4cplus/configurator.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/initializer.h>
#include <log4cplus/layout.h>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <string>
int main(int argc, char* argv[]) {
log4cplus::SharedAppenderPtr appender(new log4cplus::FileAppender("test.log"));
appender->setName("mainLog");
std::string pattern = "[%-5p][%D{%Y/%m/%d %H:%M:%S:%q}][%-l][%t] %m%n";
std::auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(pattern));
appender->setLayout(layout);
mainLog = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("mainLog"));
mainLog.addAppender(appender);
LOG4CPLUS_INFO(mainLog, LOG4CPLUS_TEXT("Execution started!"));
return 0;
}
The above code should work according to the every guide I have managed to find on the subject, but it won't compile. The line:
appender->setLayout(layout);
underscores layout and says:
cannot convert argument 1 from 'std::auto_ptr<log4cplus::Layout>' to 'std::unique_ptr<log4cplus::Layout,std::default_delete<_Ty>>
What gives?
I try to compile my code, I pretty sure I made a mistake in my headers or in the compilation but I don't understand where. I know that this is a basic problem, and I read some other topic, but I really don't understand. And I watch some other code I wrote and I don't see any difference...
g++ -c main.cpp -o out
I don't understand the error, so I also try :
g++ -c readFastqFile.cpp
The error
readFastqFile.cpp:8:1: error: ‘readFastq’ does not name a type
readFastq::readFastq(){ //Constructor
And my files are :
main.cpp
#include <iostream>
#include <string>
#include "readFastqFile.hpp"
using namespace std;
int main(int argc, char *argv[]){
cout << "hello" <<endl;
//readFastq allReads;
return 0;
}
readFastqFile.hpp
#ifdef READFASTQFILE_HPP
#define READFASTQFILE_HPP
#include <iostream>
#include <string>
using namespace std;
class readFastq{
public:
readFastq(); //constructor
private:
string readName;
string sequence;
string score;
};
#endif // READFASTQFILE_HPP
readFastqFile.cpp
#include <string>
#include <iostream>
#include "readFastqFile.hpp"
using namespace std;
readFastq::readFastq(){ //Constructor
readName = "bla";
cout << readName <<endl;
}
Thanks
#ifdef READFASTQFILE_HPP should be #ifndef READFASTQFILE_HPP. The #ifdef is causing the contents of readFastqFile.hpp to be ignored, so the class definition isn't being compiled.
See also Include guards
I am a C++ noob, fiddling with the following problem for some hours now. Hopefully, someone can enlighten me.
I had a cpp file with content like so:
test.cpp file content
#include <iostream>
#include <exception>
#include <stdlib.h>
#include <string.h>
using std::cin; using std::endl;
using std::string;
string foobar(string bar) {
return "foo" + bar;
}
int main(int argc, char* argv[])
{
string bar = "bar";
System::convCout << "Foobar: " << foobar(bar) << endl;
}
This one compiles and runs well. Now I'd like to put foobar into an external library:
mylib.h file content
string foobar(string bar);
mylib.cpp file content
#include <string.h>
using std::cin; using std::endl;
using std::string;
string foobar(string bar) {
return "foo" + bar;
}
test.cpp file content
#include <iostream>
#include <exception>
#include <stdlib.h>
#include "mylib.h"
int main(int argc, char* argv[])
{
string bar = "bar";
System::convCout << "Foobar: " << foobar(bar) << endl;
}
I adjusted my Makefile, so that test.cpp compiles and links mylib, but I always encounter the error:
test.cpp::8 undefined reference to `foobar(std::string)
How do I have to handle string arguments? My attempts seems to be completely wrong here.
Regards
Felix
The C++ standard library type std::string is in the header string. To use it, you must include <string>, not <string.h>. Your mylib.h should look something like
#ifndef MYLIB_H
#define MYLIB_H
#include <string>
std::string foobar(std::string bar);
#endif
and your mylib.cpp should include it:
#include "mylib.h"
std::string foobar(std::string bar) {
return "foo" + bar;
}
Note that it may be unnecessary to pass bar by value. Looking at your code, a const reference might do.
I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.