I have a project that has the main method accessing another method from another source file, BigDog(int). I'm pretty sure the code is right but CodeBlocks seems to not be able to detect the definition of the method unless I build the other file using debug build in CodeBlocks. In Release, I get the following error when building:
Error: undefined reference to 'BigDog(int)'
Why is that so?
main.cpp
#include <iostream>
using namespace std;
void BigDog(int KibblesCount);
int main()
{
BigDog(3);
return 0;
}
mystuff.cpp
#include <iostream>
using namespace std;
void BigDog(int KibblesCount)
{
cout << KibblesCount;
}
If you're adding a new file in codeblocks, make sure to check the checkmarks in the dialog to add it to both the debug and the release build.
Also its better practice to move your declarations to a header file and include that where needed, like this:
main.cpp:
#include "mystuff.h"
int main()
{
BigDog(3);
return 0;
}
mystuff.h:
#pragma once
void BigDog(int KibblesCount);
mystuff.cpp:
#include "mystuff.h"
#include <iostream>
void BigDog(int KibblesCount)
{
// add a newline so the line gets printed immediately
std::cout << KibblesCount << "\n";
}
Related
I've been searching through questions on this for hours, and it just isn't working for some reason, so sorry if there's already an answer and I just didn't understand it.
I'm doing a course on C++, and I've gotten to classes. As practice, he started by just calling a simple function that was defined in a separate .cpp file to show how a class would work later on. See code below:
//main.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main() {
speak();
return 0;
}
============================
//Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak() {
cout << "Meow" << endl;
}
================================
//Cat.h
#ifndef CAT_H_
#define CAT_H_
void speak();
#endif //CAT_H_
I've triple checked everything I could think of. They're in the same directory and I'm cross-referencing everything exactly as he did. I expect it to output "Meow" in the console, but if I call the speak() function in main.cpp, I still just get an empty function, despite it being defined in class.cpp. I don't know what I'm missing, and it's quite frustrating. Is is because of my compiler? I'm using Visual Studio IDE.
Edits: Used the wrong commented name
I'm using the built-in Visual Studio IDE's "Build" and "Compile" options. I'm not running the compilation using Linux commands or anything else.
#273K Here is the screenshot of my 3 VS files:
https://i.stack.imgur.com/9tbw8.png
The problem is that you are including Cat.h but named your header class.h instead of Cat.h.
To solve this rename the files from class.h and class.cpp to Cat.h and Cat.cpp respectively, as shown below
Cat.h
#ifndef CAT_H_
#define CAT_H_
void speak();
#endif //CAT_H_
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak() {
cout << "Meow" << endl;
}
main.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main() {
speak();
return 0;
}
Working demo
Thanks to #273K for the solution:
In the screenshot, the wrong project was "selected" in the solution explorer, so it was running the wrong project code.
I fixed it by right-clicking the project I wanted and clicking "Set as StartUp Project"
Before: https://i.stack.imgur.com/na0nF.png
After: https://i.stack.imgur.com/EJMLq.png
So I have 3 files in a single folder, my main file (Q1.cpp), my header file (pa2Functions.h), and my implementation file, (pa2Functions.cpp). When I call my function, I don't get any output and I am extremely confused as to why. I have included the contents of all 3 files below.
pa2Functions.h
#include <iostream>
using namespace std;
void initialize();
pa2Functions.cpp
#include <iostream>
#include "pa2Functions.h"
using namespace std;
void initialize(){
cout << "hello" << endl;}
Q1.cpp
#include <iostream>
#include "pa2Functions.h"
using namespace std;
int main(){
void initialize();
}
I compile with
g++ Q1.cpp pa2Functions.cpp -o Output
But when I run the output I just don't get anything. Any help would be greatly appreciated.
In main, remove the void in front of initialize. You're declaring the function again, not calling it.
int main(){
initialize();
}
I'm trying to learn how to utilize header files in C++ projects, so I made .cpp files containing simple functions to make sure I'm doing all the declaring and including correctly.
Everything worked fine when I only had one set of .cpp and .h files, but when I try to add more I get errors.
To start with, in my project I had:
helloworld.cpp
#include "helloworld.h"
#include <iostream>
#include <cstdio>
using namespace std;
int HelloWorld() {
puts("Hello, World!");
cout << "Hello, World!" << endl;
return 0;
}
helloworld.h
#ifndef HELLOWORLD_H_INCLUDED
#define HELLOWORLD_H_INCLUDED
int HelloWorld();
#endif /* HELLOWORLD_H_INCLUDED */
main.cpp
#include "helloworld.h"
#include <iostream>
using namespace std;
int main(){
HelloWorld();
return 0;
}
Which built with no errors and ran correctly.
Next I tried adding a second .cpp and .h file, which created building errors.
pointers.cpp
#include "pointers.h"
#include <iostream>
using namespace std;
int Pointers() {
int x = 1;
int *ptr_a = &x;
cout << *ptr_a << endl;
return 0;
}
pointers.h
#ifndef POINTERS_H_INCLUDED
#ifndef POINTERS_H_INCLUDED
int Pointers();
#endif /* POINTERS_H_INCLUDED */
and modified main.cpp:
#include "helloworld.h"
#include "pointers.h"
#include <iostream>
using namespace std;
int main(){
HelloWorld();
Pointers();
return 0;
}
Now when I try to build, I get an error saying there are multiple definitions of main -- one in main.cpp, and the other in pointers.cpp.
Even more oddly, if I make a new project and do the exact same thing but reverse the order in which I create the .cpp and .h files (i.e. pointers first then helloworld), it builds and runs correctly with just the pointers files but runs into the same error when adding helloworld files, saying that the multiple exceptions of main are in main.cpp and helloworld.cpp.
I figure it must have something to do with Eclipse itself, but I don't know what the exact issue is.
Does anyone know what might be going on?
Visual Studio is not recognising my #include 'Header.h' file. I have created the file in the Header Files in solution explorer and also tried manually pointing to the file. What I don't understand is, until yesterday there was absolutely no problem. Therefore, a simple cout doesn't work.
#include 'Header.h';
int main()
{
cout << "hi";
return 0;
}
#include "Header.h"
#include <iostream>
using namespace std;
int main()
{
cout << "hi";
return 0;
}
Not that Header.h is used in anyway, this is still the correct syntax.
You need #include <iostream> to be able to use cout.
You've got syntax errors in your #include preprocessor directive. Replace single with double quotes and drop the semicolon:
#include "Header.h"
Found the problem : I had to go to properties bar and change "Included In Folder" value to true from false.
This is really driving me crazy:
#include <iostream>
#include <vector>
#include <string.h>
#include <thread>
using namespace std;
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
int main() {
thread(test).join();
return 0;
}
The code compiles fine with the -std=c++11 flag to the compiler and the -pthread flag to the linker.
BUT: Eclipse does either know the std::thread or the myvector.begin()->length(), even if the code runs fine eclipse warns me "Method 'length' could not be resolved".
I tried every possible solution in here: Eclipse CDT C++11/C++0x support without any success. This took me so many hours now, what am I doing wrong?!
Is there anybody getting a project setup without problems with this code?
EDIT: Other code example - same problem:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
Compiles and runs correct, but returns in eclipse: Method 'test' could not be resolved
EDIT:
working versions:
((TestClass)*(testClassVector.begin())).test();
TestClass foo2 = *(testClassVector.begin());
foo2.test();
still not working:
testClassVector.begin()->test();
The last compiles and works like the two above, but eclipse still claims:
Method 'test' could not be resolved
Maybe I'm wrong, but I think your problem don't come from Eclypse. Juste, begin() on a vector return a std::vector<T>::iterator first, this is not a pointer and there is no method length, but you can ask for the vector size with myvector.size(); if this is what you want.
The problem could come from your #include <string.h> that is not the same as #include <string>, string.h is for string operation like strcmp, strstr, etc... juste string will define the std::string object.
I don't have Eclipse set up but the problem appears to be around std::string. Does the problem go away if you remove the threading from the example? (I also changed to #include <string> instead of string.h)
#include <iostream>
#include <vector>
#include <string>
#include <thread>
using namespace std;
#if 0
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
#endif
int main() {
//thread(test).join();
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
return 0;
}
That should hopefully print out 10.
Update from comment:
Does this generate the Eclipse warning?
auto tmp = *(myvector.begin());
std::cout << tmp.length() << std::endl;
What about this?
std::string foo("abc123");
std::cout << foo.length() << std::endl;
I guess one more too:
std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;
The solution found:
I downloaded eclipse kepler Kepler
Created a new project and tried to compile this source code (like above):
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
On the first run eclipse told me, thread belongs to the new c++11 standard and I have to add -std=c++11 to the compiler flags. To use thread I also added -pthread to the linker flags. With this steps the code could be compiled, but eclipse marks the thread still as unknown. To fix this I proceeded the following step:
Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs.
Found here.
Now - unbelievable but true - it works, even without any errors marked by eclipse. The solution is using the (beta) version of eclipse, wich seems to handle this in a better way.
Thanks for all your help!