How to declare and access method functions from different class files - c++

So I'm writing a POS system for a project at school and I'm having trouble declaring the call method for each file to access the said methods
main.cpp
#include <iostream>
#include <windows.h>
int main()
{
AppUI UI;
SecuritySys SecSysFunc;
EditBook BookFunc;
UI.MainMenu();
}
AppUI.cpp
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>
#include "SecuritySys.h"
#include "AppUI.h"
#include "EditBook.h"
AppUI UI;
SecuritySys SecSysFunc;
EditBook BookFunc;
using namespace std;
void AppUI::MainMenu()
{
//variable declarations
int opt;
MenuStart:
system("CLS");
TitleHeader();
setTxtColor(10);
PageTitle("Main Menu");
string Menu[] = {"Add Books", "Search Books", "View Books", "Delete Book", "Exit"};
cout << "1.\tAdd Books" << endl;
cout << "2.\tSearch Books" << endl;
cout << "3.\tView Books" << endl;
cout << "4.\tDelete Books" << endl << endl;
cout << "0.\tExit";
cout << "\n\nEnter Option: ";
cin >> opt;
switch(opt)
{
case 1:
BookFunc.AddBook();
break;
case 2:
BookFunc.AddBook();
break;
case 3:
BookFunc.AddBook();
break;
case 4:
BookFunc.AddBook();
break;
case 0:
SecSysFunc.Logout();
break;
default:
cout << "Invalid option!";
sleep(1);
goto MenuStart;
}
}
AppUI.h
#ifndef APPUI_H
#define APPUI_H
class AppUI
{
public:
void MainMenu();
void AddBook();
void SearchBook();
private:
};
#endif // APPUI_H
EditBook.cpp
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>
#include "SecuritySys.h"
#include "AppUI.h"
#include "EditBook.h"
AppUI UI;
SecuritySys SecSysFunc;
EditBook BookFunc;
using namespace std;
void EditBook::AddBook()
{
system("CLS");
UI.AddBook();
}
SearchByTitle(string searchTxt)
{
cout << "Enter search by title code here";
system("pause");
UI.MainMenu();
}
EditBook.h
#ifndef EDITBOOK_H
#define EDITBOOK_H
class EditBook
{
public:
void AddBook();
void SearchBook();
void ViewBooks();
void DeleteBooks();
protected:
private:
};
#endif // EDITBOOK_H
SecuritySys.cpp
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <stdio.h>
#include "SecuritySys.h"
#include "AppUI.h"
#include "EditBook.h"
AppUI UI;
SecuritySys SecSysFunc;
EditBook BookFunc;
using namespace std;
void SecuritySys::Login()
{
UI.MainMenu();
}
void SecuritySys::Logout()
{
exit(0);
}
SecuritySys.h
#ifndef SECURITYSYS_H
#define SECURITYSYS_H
class SecuritySys
{
public:
void Login();
void Logout();
private:
};
#endif
When I add AppUI UI; SecuritySys SecSysFunc; EditBook BookFunc; to the top of AppUI.cpp EditBook.cpp and SecuritySys.cpp I get an error saying multiple definition of UI,SecSysFunc and the BookFunc. But if I don't add them at the top, then the BookFunc.AddBook(); for example, the method can't be recognized and therefor can't be called or used. What am I doing wrong here?

You are getting a multiple definition error because you are just redeclaring the objects UI, SecSysFunc and BookFunc. You need to make it clear to the compiler that these identifiers point to the same object. You can do so by picking one of the four declarations to be the definition, leave those lines as is. But to the declarations in other code files add extern before the declaration. This will tell the compiler to look in other linked object files for the definition of those objects

Related

Having issues with mmap from within a function

I'm trying to write a function that has mmap within it, however when I try to access the memory from main(), it gets a segfault. Does anyone have any idea why?
Please ignore the MPI headers - it's for the later part of the project. I have commented out the mprotect line to see that it is an mmap fault as opposed to the handler not necessarily working
net_map.cpp:
#include <iostream>
#include <signal.h>
#include <sys/mman.h>
#include <mpi.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "net_map.h"
using namespace std;
void handler(int sig, siginfo_t* info, void* other)
{
void* address = info->si_addr;
cout << "\nSegfault Detected! [Address: " << address << "]\n" << endl;
int unprotectresult = mprotect(address, SIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
if (unprotectresult == 0)
{
cout << "\nSuccessful mprotect (memory unprotected)!\n" << endl;
}else
{
cout << "\nMprotect unsuccessful\n" << endl;
}
}
void netmap (char filename[], char* mapped)
{
int file;
file = open(filename, O_RDWR);
mapped = (char*) mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, file, 0);
if (mapped == MAP_FAILED)
{
cout << "\nMap Failed!" << endl;
}
cout << mapped << endl;
}
main.cpp
#include <iostream>
#include <signal.h>
#include <sys/mman.h>
#include <mpi.h>
#include <unistd.h>
#include <array>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <array>
#include "net_map.h"
using namespace std;
int main(int argc, char* argv[])
{
struct sigaction segaction;
memset(&segaction, 0, sizeof(segaction));
segaction.sa_flags = SA_SIGINFO;
sigemptyset(&segaction.sa_mask);
segaction.sa_sigaction = handler;
sigaction(SIGSEGV, &segaction, NULL);
int i;
char* mapped;
networkpage sheet;
char filename[] = "hello.txt";
netmap(filename, mapped);
sheet.address = (void*)mapped;
cout << "\nAddress: " << sheet.address << endl;
//mprotect(sheet.address, SIZE, PROT_NONE);
memcpy(sheet.address, "k", 1);
munmap(sheet.address, SIZE);
return 0;
}

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;
}

How to connect two modules by systemC tlm_fifo<float>?

I am still a novice for writing systemC-TLM. Now, I want to connect two modules by tlm_fifoFF. I am searching examples for a long time on net. But no use. Please help to give some ideas or examples how to achieve this.
This is my architecture:
and,this is my error information:
this is "main.cpp"
#include <systemc.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "merlin2.h"
#include "tlm.h"
#include <tlm_utils/simple_target_socket.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_channels/tlm_fifo/tlm_fifo.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_1_interfaces/tlm_fifo_ifs.h>
using namespace sc_core;
using namespace tlm;
using namespace tlm_utils;
using namespace std;
int sc_main(int argc, char** argv){
Merlin2 merlin_test("merlin2_JW");
sc_start();
cout << "end" << endl;
return 0;
}
this is "merlin2.cpp"
#include "merlin2.h"
Merlin2::Merlin2(const sc_module_name& name)
{
ProcessEngine PE_TEST("test_PE");
test_PE TP("PE_test");
tlm::tlm_fifo <float> FF ("fifo");
TP.out(FF);
PE_TEST.in(FF);
}
this is "merlin2.h"
#ifndef __MERLIN2_H__
#define __MERLIN2_H__
#include <fstream>
#include <string>
#include <systemc.h>
#include "pe.h"
#include "test_PE.h"
class Merlin2: public sc_module
{
public:
SC_HAS_PROCESS(Merlin2);
Merlin2(const sc_module_name& name);
};
#endif
this is "pe.cpp"
#include <systemc.h>
#include <iostream>
#include "pe.h"
using namespace std;
ProcessEngine::ProcessEngine(const sc_module_name& name):in("in")
{
cout << "before SC_THREAD(run)" << endl;
SC_THREAD(run);
cout << "after SC_THREAD(run)" << endl;
}
void ProcessEngine::run()
{
int N = in.nb_get();
cout << " N=" << N << endl;
wait(1,SC_NS);
}
this is "pe.h"
#ifndef __PE_H__
#define __PE_H__
#include <iostream>
#include <bitset>
#include "tlm.h"
#include <tlm_utils/simple_target_socket.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_channels/tlm_fifo/tlm_fifo.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_1_interfaces/tlm_fifo_ifs.h>
using namespace sc_core;
using namespace tlm;
using namespace tlm_utils;
using namespace std;
class ProcessEngine: public sc_module
{
public:
tlm::tlm_fifo<float> in;
void run();
ProcessEngine(const sc_module_name& name);
SC_HAS_PROCESS(ProcessEngine);
};
#endif
this is "test_PE.cpp"
#include <systemc.h>
#include <iostream>
#include "test_PE.h"
using namespace std;
test_PE::test_PE(const sc_module_name& name):out("out")
{
cout << "before start_test " << endl;
SC_THREAD(start_test);
cout << "After start_test " << endl;
}
void test_PE::start_test()
{
float A=5;
in.nb_put(A);
cout << "A=" << A << endl;
wait(1,SC_NS);
}
this is "test_PE.h"
#ifndef __TESTPE_H__
#define __TESTPE_H__
#include <fstream>
#include <string>
#include <systemc.h>
#include "tlm.h"
#include <tlm_utils/simple_target_socket.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_channels/tlm_fifo/tlm_fifo.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_1_interfaces/tlm_fifo_ifs.h>
using namespace sc_core;
using namespace tlm;
using namespace tlm_utils;
using namespace std;
class test_PE:public sc_module
{
public:
tlm::tlm_fifo<float> out;
test_PE(const sc_module_name& name);
SC_HAS_PROCESS(test_PE);
void start_test();
private:
float A;
};
#endif
You're mixing SystemC/TLM notions and especially ports and channels.
tlm_fifo is a channel. It can be used to connect two ports. It's the yellow part of your image.
However, you can't directly connect a channel to a module. Instead, you need to use ports in each module you want to connect the channel. A port is associated to an interface defining methods you can use. In your case, you use nb_get(...) and nb_put(...).
For tlm_fifo, you can use these ports:
sc_port< tlm_nonblocking_put_if<float> >
sc_port< tlm_nonblocking_get_if<float> >
Finally, you create instances of your modules in your Merlin2 constructor. Objects are not persisted and deleted at the end of the constructor. They should be member of the class. Here is a complete working example of TLM-1.0 using tlm_fifo with non blocking interface.
Note: You're mixing TLM-2.0 and TLM-1.0 headers. I removed useless headers.
main.cpp
#include <systemc>
#include "merlin2.h"
using namespace sc_core;
using namespace std;
int sc_main(int argc, char** argv) {
Merlin2 merlin_test("merlin2_JW");
sc_start();
cout << "end" << endl;
return 0;
}
merlin2.h
#ifndef __MERLIN2_H__
#define __MERLIN2_H__
#include <fstream>
#include <string>
#include <systemc>
#include "pe.h"
#include "test_PE.h"
class Merlin2: public sc_module
{
public:
Merlin2(const sc_module_name& name);
tlm::tlm_fifo <float> FF;
ProcessEngine PE_TEST;
test_PE TP;
};
#endif
merlin2.cpp
#include "merlin2.h"
Merlin2::Merlin2(const sc_module_name& name):
PE_TEST("test_PE"),
TP("PE_test"),
FF("fifo")
{
TP.out(FF);
PE_TEST.in(FF);
}
pe.h
#ifndef __PE_H__
#define __PE_H__
#include <fstream>
#include <string>
#include <systemc>
#include <tlm>
using namespace sc_core;
using namespace tlm;
using namespace std;
class ProcessEngine: public sc_module
{
public:
sc_port< tlm_nonblocking_get_if<float> > in;
void run();
ProcessEngine(const sc_module_name& name);
SC_HAS_PROCESS(ProcessEngine);
};
#endif
pe.cpp
#include <systemc.h>
#include "pe.h"
using namespace std;
ProcessEngine::ProcessEngine(const sc_module_name& name):
in("in")
{
SC_THREAD(run);
}
void ProcessEngine::run()
{
sc_core::wait(1, SC_NS);
float N = 0;
bool r = in->nb_get(N);
cout << " N=" << N << endl;
}
test_PE.cpp
#include <systemc>
#include "test_PE.h"
using namespace std;
test_PE::test_PE(const sc_module_name& name):
out("out")
{
SC_THREAD(start_test);
}
void test_PE::start_test()
{
float A=5;
out->nb_put(A);
cout << "A=" << A << endl;
sc_core::wait(1, SC_NS);
}
test_PE.h
#ifndef __TESTPE_H__
#define __TESTPE_H__
#include <fstream>
#include <string>
#include <systemc>
#include <tlm>
using namespace sc_core;
using namespace tlm;
using namespace std;
class test_PE:public sc_module
{
public:
sc_port< tlm_nonblocking_put_if<float> > out;
test_PE(const sc_module_name& name);
SC_HAS_PROCESS(test_PE);
void start_test();
private:
float A;
};
#endif

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()