Undefined reference to function which is member of an another class - c++

I'm building a simple application to solve sudoku puzzles. It's my first time creating something serious with C++, so I'm open for code style/structure critique.
The problem I bumped into has something to do with organizing multiple files.
I have two classes referencing each other using functions. When I try to call a function:
void Field::runColumnCheckout(CellGroup* sender, int cellRelativeX)
{
}
in a CellGroup class using an instance of Field class:
void CellGroup::runISC(int possibilityNumber)
{
for (int x = 0; x < 3; x++) {
int amountInColumn = 0;
for (int y = 0; y < 3; y++)
if (cells[x][y]->isPossible(possibilityNumber))
amountInColumn++;
if (amountInColumn > 1) {
//parentField is an instance of a Field class
//stored in a private field of the CellGroup class
parentField->runColumnCheckout(this, x);
return;
}
}
//...
}
An undefined reference occures. I don't quite understand, why.
All the examples were taken from cell_group.cpp and field.cpp that are defining classes from cell_group.h and field.h.
Unfortunately, I couldn't manage to put all the files in a question as they have gained a lot of lines, but you can look at them on my github.
I have found a similar question and another one, but they seem to have issue with the way they compile their files. I've had two referencing each other classes structured similarly before adding code I have problem with right now and everything compiled fine.
I'm compiling everything using GCC compiler on Windows and CodeBlocks as an IDE.

CodeBlocks somehow lost connection with field.cpp file or didn't add it creating file. Going and manually adding it to the project tree (even though it was already there) Project->Add files... solved the problem.
Thanks #Peter for finding the solution.

Related

Question about ISupportErrorInfo::InterfaceSupportsErrorInfo() implementation

I am a babysitter now overlooking a huge old-time (started in last century) code base for Windows, using C++ and C#, largely using COM as a glue.
I noticed that ISupportErrorInfo::InterfaceSupportsErrorInfo() implementations (around 400 of them, wow) are just naive, exactly as here:
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
{
static const IID* arr[] =
{
&IID_IStore,
};
for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
{
if (InlineIsEqualGUID(*arr[i], riid))
return S_OK;
}
return S_FALSE;
}
It looks to me that, once upon a time, someone (MVP, evangelist) has written a book about COM and then everybody later just copied these lines without thinking.
First of all, why do I need an array if I only have one item in it? Without an array, I don't need a for-loop.
To me, it looks like one line will be fine, eg.
return MyCoolHelper::InterfaceSupportsErrorInfo(riid, IID_IStore);
Maybe you have seen a decent implementation of it that supports 1 or more IID-s?

Cannot access member functions of vector<int>

I've been attempting to figure out this bug for about an hour now. It's probably a really obvious syntax thing I'm overlooking. This is my first C++ project, and I don't have a good handle on the structure of the language.
Here's my header file:
#pragma once
#include <vector>
class BoardState
{
private:
std::vector<int> numbers;
int SIZE;
public:
BoardState();
std::vector<int> getState();
bool isZero();
};
And here's the implementation, in a separate file:
#include "BoardState.h"
BoardState::BoardState(){
SIZE = 4;
numbers.push_back(1);
numbers.push_back(3);
numbers.push_back(5);
numbers.push_back(7);
}
std::vector<int> BoardState::getState() { return numbers; }
bool BoardState::isZero() {
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i] != 0) { return false; }
}
return true;
}
This code is really simple, so I have no clue what could be going wrong to produce the errors. However, on every method call, push_back and size, I am getting errors, saying that class "std::vector<int, allocator>" has no member "method_name_here".
My background is Java, so my first thought was that I wasn't able to call these methods because numbers is not initialized. However, any attempt I made to initialize numbers in the header file resulted in an error as well. I tried std::vector<int> numbers = { 1,3,5,7 };, I tried std::vector<int> numbers(4,0);, I even tried creating an array and constructing the vector from that. Not only did all those attempts cause errors, they also didn't fix the method calls either.
What am I missing? Do I need to initialize the vector, or is what I have in the header file enough? Any advice would be helpful here, since I can't find anything online about similar errors. I've even copy-pasted code from StackOverflow answers about similar problems, and that produced errors as well.
EDIT: I've pared down the code as much as possible while keeping the error:
#include <vector>
class BoardState
{
std::vector<int> numbers;
BoardState() { numbers.push_back(1); }
int getSize() {
int i = numbers.size();
return i;
}
};
On the line numbers.push_back(1);, my compiler underlines the token "push_back", and highlighting it reads:
class "std::vector<int, allocator>" has no member "push_back"
On the line int i = numbers.size();, the token "size" is underlined, and the error reads:
class "std::vector<int, allocator>" has no member "size"
I still have no clue what's going on.
Edit 2: Put the method calls into a constructor and a function. This changed the error message associated with push_back().
Edit 3: I have discovered something very disconcerting. This code works perfectly fine in a different compiler. I copy-pasted in the exact code from Edit 1 and it ran with no issues. I think the problem must be with Visual Studio rather than the actual code. Thank you all for helping me out with this. I think I'm just going to switch to a different compiler and hope for the best.
Edit 4: Just to prove to pm100 that my code is exactly as I've said, here's a screenshot from visual studio.
Here it is.
Aside from the main method, this is character-for-character what I've put in this question. I have a guess as to why this doesn't work, though. I modified my version of Visual Studio 2019 to run .386 assembly code for a college class. While I think I followed the guide to do that without affecting anything else, it may have screwed up parts of the C++ compiler.
I suggest that you could select Tools->Import and Export Settings->Reset all settings-> Visual C++ to restore the default settings.
If it does not work, you could reinstall VS.

Logical operator causing compile-time error on functions called

This question might have been answered before, but searching around and using google didn't bring me there, so I'll ask.
I'm currently making a game and working on collision, however, for some reason it seems like whenever I try to compile I get a ISO C++ forbids comparison between pointer and integer
At first I thought I might have something wrong in my syntax which I checked, but wasn't able to find anything, so I just left the line of code to this:
if((getMinX() > c->getMinX()) && (getMinX() < c-getMaxX()))
I tried adding this-> or adding parenthesis, but that doesn't seem to work, however, just doing
if(this->getMinX() > c->getMinX())
seems to work fine as well as
if((5 > 3) && (5 < 10))
Is there something I'm missing?
Both objects are derived from a class called Collideable defined like this
class Collideable
{ public:
bool collidesWith(Collideable*);
virtual int getMinX() = 0;
virtual int getMaxX() = 0;
virtual int getMinY() = 0;
virtual int getMaxY() = 0;
};
All classes properly override from the virtual methods and the code causing the issue is in bool Collideable::collidesWith(Collideable* c)
According to http://www.cplusplus.com/doc/tutorial/operators/ the logical operators return a boolean value (which makes sense to me, coming from Java) so what's causing this problem?
It is difficult to know if this is what is in the program or a simple transcription problem:
The first line is written ending as c-getMaxX(); maybe it should be c->getMaxX()?
You have c-getMaxX() but you need c->getMaxX(), with a >.

Access pre-compiled functions within a class C++/11

Sorry if the title is misleading, I'm currently looking for solutions to the following:
I'm developing a library, for other people to use. They have to follow a strict design concept and the way they structure any additional features within the library. They all use Linux and (Vim) and as such as are allowed to use terminal commands (i.e to be able to compile etc..) and we all use clang as a compiler.
My question is this: Let's suppose I write a function called: "checkCode":
template<typename T>
void checkCode(T&& codeSnippet)
{
//// code
}
I want to make this function run so whenever they type "checkCode" in a terminal this function is therefore called. I know using clang thy have similar functionality, however, this is understandable as you're using the whole of clang. So:
1) Is it possible to just compile a class, and then access each of the functions through
the .dylab | .so file?
2) Might it be a better idea, or, better to take a copy of the source of clang, add this functionality and role it out to those using and contributing to the library? This would be like an additional add-on to clang?
Thanks
you could use one executable and symbolic links to it like busybox:
int main(int argc, char **argv)
{
string programName = argv[0];
size_t lastSlash = programName.find_last_of('/');
if(lastSlash != string::npos)
programName = programName.substr(lastSlash + 1);
if(programName == "function_1")
{
function_1();
return 0;
}
if(programName == "function_2")
{
function_2();
return 0;
}
// ...
// normal main code
return 0;
}

if statement in main function c++

Okay, probably a dumb question to you guys but I can't figure it out.
So I'm taking a c++ basics course in class and so far I'm struggling/crying.
I can't show you guys my code because I'm not allowed/there are consequences if I'm caught but I could probably give a example.
I'm using xcode. So when I compile, I get two errors below (image provided).
I searched for similar questions, but those seem too complex compared to what I'm doing. In addition, the only includes I have are iostream and string.
I know the problem occurs when I add an if statement in my main function. I know this because when I delete it, everything compiles as expected. Yet when I add it again to the main function, these errors occur.
So my question is, based on what I know, is it proper to add an if statements whenever in the main function?
Below is an example. I wrote the functions below and called above.
#include <iostream>
#include <string>
using namespace std;
// example functions that I just made up to explain the structure of my actual code.
//Don't bother trying to understand it. It's just to explain that
//I wrote my functions at the
// bottom and called it at the top.
int getNumberofWins(param1, param2);
string getTheName(int player1);
int executeCycle(string p1_name, string p2_name);
void stateWinner(string winner_name);
int main {
playerOne = getTheName(1);
playerTwo = getTheName(2);
r1 = executeCycle(playerOne, playerTwo);
r2= executeCycle(playerOne, playerTwo);
totalWin1 = getNumberOfWins(1, r1, r2);
totalWin2 = getNumberOfWins(2, r1, r2);
cout << totalWin1;
//This is the where I get the errors. When I delete the if statement,
//Everything compiles. When I add it, an error occurs.
if (totalWin1 == 2){
stateWinner(playerOne);
}
return 0;
}
string getTheName(int player1){
string playerOne;
string playerTwo;
if(player_number == 1){ code code code
}
}
int getNumberofWins (int param1, int param2){
code code code
}
int executeCycle(string p1_name, string p2_name){
code code code
}
void stateWinner(string winner_name){
if(!winner_name.empty()){
code code code
}
I hope it's fine if the code above isn't accurate. I think the point is that once I add my if statement to the main function, the two errors show up.
actually...now that I look at it, they both seem like similar errors. I just don't know why they both appear...
Sorry if this is an obvious answer or if it isn't clear.
The "announceWinner" function is not defined anywhere, ie there's no
void announceWinner () {
// code
}
anywhere. Either you haven't written it yet, or the file that contains it is not being compiled & linked with the main program.