error: 'int main(int, char**)' previously defined here in C++ - c++

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.

Related

Log4cplus not compiling as descirbed

#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?

Optimize source structure for C++ projects

I would like to create a little project, which is divided in more then one file.
main.cpp:
#include <cstdlib>
#include <iostream>
#include sc_hpp
using namespace std;
int main(int argc, char *argv[])
{
add(3,4);
system("PAUSE");
return EXIT_SUCCESS;
}
sc.hpp:
#ifndef "sc.hpp"
#define sc_hpp
int add(int a, int b);
#endif
function.cpp:
#include "sc.hpp"
int add(int a, int b)
{
return(a + b);
}
But it doesn't work.
ERROR:
`add' undeclared (first use this function)
First time I'm trying make programme in more then one file, so I think the problem must be easy to solve.
You have two obvious mistakes:
In your main:
#include <cstdlib>
#include <iostream>
// the included header file needs to be enclosed in " "
// and it needs a suffix, i.e.: `.h`
#include "sc_hpp.h"
using namespace std;
int main(int argc, char *argv[])
{
}
In sc.hpp:
// the include guards doesn't have to be enclosed in " "
// the suffix's dot:'.' is replaced with underscore: '_'
// header name in uppercase letters
#ifndef SC_HPP_H
#define SC_HPP_H
int add(int a, int b);
// included .cpp files with function implementation here
#include "sc.hpp"
#endif
More on how to organize code files, here.
In general the preprocessor directive #include expands the code contained in the file that follows it, so your code in main looks like this:
#include <cstdlib>
#include <iostream>
// #include "sc_hpp.h" replaced with
int add(int a, int b);
// the #include "sc.cpp" nested within the "sc_hpp.h" is replaced with
int add(int a, int b)
{
return(a + b);
}
using namespace std;
int main(int argc, char *argv[])
{
}

OpenCV undefined reference to my own method in library

I wrote a piece of software in C++ OpenCV, that's so structured:
main.cpp
testfps.cpp
testfps.hpp
The problem is that I get the these two errors
undefined reference to "myTestfps1(int, int)"
undefined reference to "myTestfps2(int, int)"
These two method are written in testfps.cpp and declared in testfps.hpp.
In main.cpp are declared all the necessary #include <name>, and after them there is #include "testfps.hpp"
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "testfps.hpp"
int main(int argc, char** argv[]){
....
switch(c){
case 1: myTestfps1(a,b);break;
case 2: myTestfps2(a,b);break;
}
...
}
testfps.cpp
#include <opencv2/opencv.hpp>
#include <time.h>
#include <stdio.h>
#include "testfps.hpp"
int myTestfps1(int a, int b){
...
}
int myTestfps2(int a, int b){
...
}
testfps.hpp
#ifndef TESTFPS_HPP
#define TESTFPS_HPP
int myTestfps1(int a, int b);
int myTestfps2(int a, int b);
#endif
What's wrong with this?
The main issue you might be having, as Samuel pointed out, is that you're not including testfps.cpp in your compilation.
If you're running on linux with g++, try this:
g++ -o testfps main.cpp testfps.cpp
In any case, since you're working on C++ I recommend you don't use C headers such as stdio.h and stdlib.h. Instead use cstdio and cstdlib:
#include <cstdlib>
#include <cstdio>
Finally, your definition of main is wrong, argv is not a pointer3:
int main(int argc, char* argv[])
or:
int main(int argc, char** argv)

Compilation error when including boost/program_options.hpp

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.

variable was not declare in the scope class in private can not be access by member function

//This is the header file (header.h)
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
~
~
//This is the cpp file (program.cpp)
#include "header.h"
#include <cstring>
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
I'm getting program.cpp:13: error: 'w' was not declared in this scope
I'm trying to just do the strcpy from data which contain some info to w which is from the private section of the class and using the member function to access them.
I'm not sure if I forgot anything and why I can't access them.
Thanks to the last answer from Sergey Vakulenko
The sequence of the header file is very important.
It should be
#include <cstring>
#include "header.h"
not
#include "header.h"
#include <cstring>
add these headers to your cpp file:
#include <stdio.h>
#include <string.h>
#include "nameofheader.h"
Edit (more full explication ):
for me, that exemple not give any error:
1.h:
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
1.cpp:
#include <stdio.h>
#include <string.h>
#include "1.h"
//This is the cpp file (program.cpp)
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
int main (int argc, char** argv) {
return 0;
}
compled with g++:
g++ 1.cpp -o 1
Your program, the way you are showing it to us here, should compile without problems:
ideone.com/Bj6VU
If you want more help, you should make the all of the two files you are compiling (program.cpp and header.h) available.