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;
}
Related
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
I need to parse json in my C++ program. I decided to use RapidJson library for this purpose, but I got "abort() has been called" error. I truncated the code to this:
#include <iostream>
#include <cstdlib>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/encodings.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
wchar_t request[] = L"{\"result\":\"OK\"}";
int main()
{
WDocument d;
d.Parse(request);
WValue& v = d[L"result"]; // The exception throws here
if (wcscmp(v.GetString(), L"OK"))
{
cout << "Success!" << endl;
}
else
cout << "Fail!" << endl;
system("pause");
return 0;
}
but I got the error again. Where is the mistake? Thanks in advance!
check this line:
wchar_t request[] = L"{\"result\":\"OK\"}";
there is a character before the left brace.
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
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()
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
}