undefined refereance to class::function error - c++

I am lost and ran out of things to try that I know. I am trying to call a function in my main and I keep getting an undefined reference error (undefined reference to 'ParkingController::parkingMenuControl()').
How can I solve this issue? I have tried to rename that call to the class and even tried to rename the class and non of that has worked. Below is my code. Thank you in advance for help.
ParkingConrtol.h
#ifndef PARKINGCONTROL_H_INCLUDED
#define PARKINGCONTROL_H_INCLUDED
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
class ParkingController
{
public:
void parkingMenuControl();
int parkingOption;
private:
};
#endif
parkingControlMain.cpp
#include "parkingControl.h"
using namespace std;
ParkingController parkingMenuMain;
int main(){
cout << "Welcome, Would you like to view our menu for parking?"<< endl;
cout << "Enter 1 to view menu or 0 to leave." << endl;
cin>>parkingMenuMain.parkingOption;
if (parkingMenuMain.parkingOption == 0)
{
exit(-1);
}
else if (parkingMenuMain.parkingOption == 1)
{
parkingMenuMain.parkingMenuControl();
}
}
and lastly here is my function parkingControl.cpp
#include "parkingControl.h"
/*
This module sets up the menu for the parking controller.
In here one can view the drawer with the money stored in
it and select a parking gate to enter from.
*/
using namespace std;
ParkingController parkingControlMenu;
void ParkingController::parkingMenuControl()
{
//Doing some function
}

Related

c++ getter not returning value (NULL)

when i try to cout the get function it don't return any value !
my header file :
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Dog
{
private:
string dogName = "Max";
public:
Dog();
void talking();
void jumping();
void setDogName(string newDogName);
///////thats the function /////
string getDogName();
};
my cpp file :
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
Dog::Dog() {
cout << " Dog has been Created " << endl;
}
void Dog::setDogName(string newDogName)
{
newDogName = dogName;
}
string Dog::getDogName()
{
return dogName;
}
and my main.cpp file:
#include "stdafx.h"
#include <iostream>
#include "Dog.h"
#include "Cat.h"
int main()
{
Dog ewila;
ewila.setDogName("3wila");
cout << ewila.getDogName();
return 0;
}
im learning c++ new so i don't know whats happening even i tried to type ewila.getDogName(); by itself and it didn't do anything
and i tried to store the setDogname() in a variable to return with the getDogName() and also nothing i don't know what i'm doing wrong
also : im using visual studio 2017 and i run the program from visual c++ 2015 MSBuild command prompt
The problem is in line newDogName = dogName; of function void Dog::setDogName(string newDogName). You are using assignment operator(=) incorrectly. This should be dogName =newDogName;
So the corrected CPP file will be:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
Dog::Dog() {
cout << " Dog has been Created " << endl;
}
void Dog::setDogName(string newDogName)
{
dogName = newDogName;
}
string Dog::getDogName()
{
return dogName;
}

"endl" causes "C1001" error

My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...

C++ Headers & Undefined Reference

So I'm just getting to grips with C++ and progressing onto using header files. Thing is, I'm totally confused. I've read a documentation but none to make me understand it well enough.
I'm just making a silly 'game' which is interactive, it will probably be discarded and thought I could practice use on header files. This is my file structure:
terminal_test
├── core.cpp
└── game
├── game.cpp
└── game.h
Now, here is my core.cpp:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include "game/game.h"
using namespace std;
void mainMenu();
void rootInterface() {
cout << "root#system:~# ";
}
int main() {
system("clear");
usleep(2000);
mainMenu();
return 0;
}
void mainMenu() {
int menuChoice = 0;
cout << "[1] - Start Game";
cout << "[2] - How To Play";
cout << endl;
rootInterface();
cin >> menuChoice;
if ( menuChoice == 1 ) {
startGame();
} else if ( menuChoice == 2 ) {
cout << "This worked.";
}
}
Everything else works fine but startGame(); under my menu choice. When I compile using g++ core.cpp game/game.cpp it bounces back with this error: undefined reference to startGame();. I firstly did some troubleshooting to see if it was properly finding game.h by changing the #include "game/game.h" to something like #include "game.h" without the directory listed inside and it gave me a game.h could not be found so I know it's recognising it, just not compiling at all.
Here is my game.h:
#ifndef GAME_H // Making sure not to include the header multiple times
#define GAME_H
#include "game.h"
void startGame();
#endif
game.cpp:
#include <iostream>
#include <stdio.h>
#include "game.h"
int main(int argc, char const *argv[]) {
void startGame() {
cout << "It worked.";
}
return 0;
}
My file structure isn't named properly either, I just threw it in because it was something to just get to grips with header files in C++.
So, here are my questions:
1) - What is this error specifically saying and what should I do to fix it?
2) - How do header files communicate and work with other files and is there clear documentation/guides out there that can help?
Local function definitions are not what you want here:
#include <iostream>
#include <stdio.h>
#include "game.h"
int main(int argc, char const *argv[]) {
// an attempt to define startGame() inside of main()
void startGame() {
cout << "It worked.";
}
return 0;
}
main is not needed in your game.cpp file. You should define startGame() outside of main, like this:
#include <iostream>
#include <stdio.h>
#include "game.h"
// definition of startGame
void startGame() {
cout << "It worked.";
}

Visual Studio 2015: Getting "non standard syntax, use '&' to create a pointer to member" error

Super confused as to what is throwing the error when I try to compile my code. I'm currently trying to test a function I wrote by printing out the values it should extract from a file.
gameboard.cpp
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include "error.h"
using namespace std;
int boardDim(ifstream & inputFile, unsigned int x, unsigned int y) {
inputFile.open; //error is thrown here
if (!(inputFile.is_open())) {
throw fileNotOpen;
}
else {
stringstream output(inputFile.getline); //error is also thrown here
if (output >> x) {
if (output >> y) {
return success;
}
return secBoardVarErr;
}
return firstBoardVarErr;
}
cout << x << endl;
cout << y << endl;
}
gameboard.h
#ifndef GAMEBOARD_H
#define GAMEBOARD_H
#include <iostream>
using namespace std;
//takes in dimensions of board from file
int boardDim(ifstream &, unsigned int, unsigned int);
#endif !GAMEBOARD_H
main function
#include "stdafx.h"
#include "functions.h"
#include "gamepieces.h"
#include "gameboard.h"
#include "error.h"
int main(int argc, char * argv[])
{
ifstream x("test.txt");
int test = 0;
cout << boardDim(x, 0, 0) << endl;
return success;
}
I'm only testing the function I declared and defined in the gameboard header and source files, so the other included files will be used in the future but have already been tested and are not throwing errors when I compile and run it.
Thank you!
inputFile.open is a function, same with inputFile.getline, so what you have here is a syntactic error. The correct syntax is:
inputFile.open()
and
inputFile.getline()

NetBeans not recognizing C++ class members at build+run

I am still fairly new to NetBeans, and am writing code for class in C++. I am currently on my third project, and I have run into an error I can't seem to resolve when trying to compile+run my project. I have quadruple-checked my code, going so far as to copy code from a previous project. I have tried quiting, rebooting the computer, and starting NetBeans up again. I ran CppCheck on my code and it found no errors.
The error message:
build/Debug/MinGW-Windows/main.o: In function `main':
C:/Users/Martin/Documents/NetBeansProjects/Lab3/main.cpp:52: undefined reference to `Dictionary::Dictionary()'
C:/Users/Martin/Documents/NetBeansProjects/Lab3/main.cpp:52: undefined reference to `Dictionary::~Dictionary()'
I tried copying code from a previous project, and even with the exact same code as a previous project which works, it's still having this problem. Basically, the build is failing to recognize the Dictionary class.
What things can I check that might cause this problem? Any obscure (or even obvious) settings I can check? Should I just start a new project and copy my code over?
Edit: Adding main():
#include <cstdlib>
#include <iostream>
#include "Dictionary.h"
using namespace std;
/*
* argv[1] dictionary file
* argv[2] boggle board file
* argv[3] output file
*/
int main(int argc, char** argv) {
if (argc > 3) {
Dictionary dict;
dict.loadDictFile(argv[1]);
} else {
cout << "Not enough arguments. Needed: ./lab3 [dictionary file] "
"[board file] [output file]" << endl;
}
return 0;
}
And Dictionary.h:
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include <set>
using namespace std;
class Dictionary {
public:
Dictionary();
Dictionary(const Dictionary& orig);
virtual ~Dictionary();
virtual void loadDictFile(char * fileName);
virtual bool find(string word);
private:
set<string> dict;
set<string> fullDictionary; // Contains all words, not just those 4+ char long.
};
#endif /* DICTIONARY_H */
And Dictionary.cpp:
#include "Dictionary.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <set>
//using namespace std;
Dictionary::Dictionary() {
}
Dictionary::Dictionary(const Dictionary& orig) {
dict = orig.dict;
fullDictionary = orig.fullDictionary;
}
Dictionary::~Dictionary() {
}
void Dictionary::loadDictFile(char* fileName) {
ifstream infile;
infile.open(fileName);
if (infile) {
while(!infile.eof()) {
string line;
getline(infile, line);
fullDictionary.insert(line);
if (line.size() > 3) {
dict.insert(line);
}
}
} else {
cout << "Dictionary File not loaded: " << fileName << endl;
}
}
bool Dictionary::find(string word){
if (dict.find(word) != dict.end()) {
return true;
} else {
return false;
}
}
Found my problem. Netbeans didn't consider the Dictionary class to be part of my project, so it wasn't compiling Dictionary.cpp. I added it in the Project window by right-clicking the Source Files folder and using Add existing item... menu option. Now it compiles fine.
Does anyone know why the class wouldn't be added if I used Netbean's New File interface and added to the project specifically?