I have a programming assignment where I'm supposed to write up the code for inserting and removing linked lists. However I haven't used C++ in a while and am struggling remember certain things.
Right now, I am simply trying to put a prototype method in a header file, define it in my cpp file, and then call it in my main method. this is what I have.
LinkedList.h
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
LinkedList.cpp
#include "LinkedList.h"
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
I am getting the following errors
a nonstatic member reference must be relative to a specific object
'LinkedList::testPrint': non-standard syntax; use & to create a pointer to member
LinkedList::testPrint() is a member function.
It is not declared static, so that means it must be called on a particular object, defined as LinkedList linked_list, for example. Then use linked_list.testPrint().
Option 1 - static member function declaration
#include <iostream>
using namespace std;
class LinkedList {
public:
static void testPrint();
};
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
Option 2 - Instantiated object with call to member function
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
int main() {
LinkedList linked_list;
linked_list.testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
Related
Using the RPI Pico SDK, I have three files.
I want to use a callback function and access private members of a class.
The callback function is in an SDK and I can not modify it.
How do I do this?
/////// test.h
bool main_loop_timer_callback(struct repeating_timer *t);
class MyClass {
private:
static int count;
struct repeating_timer main_loop_timer;
public:
MyClass();
friend bool main_loop_timer_callback(struct repeating_timer *t);
};
//////// test.cc
#include <iostream>
#include "pico/stdlib.h"
#include "test.h"
using namespace std;
bool main_loop_timer_callback(struct repeating_timer *t) {
MyClass::count++;
cout << "callback " << MyClass::count << endl;
return true;
}
MyClass::MyClass() {
add_repeating_timer_us(-50000,
main_loop_timer_callback,
NULL,
&main_loop_timer);
count = 0;
};
/////// test-main.cc
#include "pico/stdlib.h"
#include "test.h"
using namespace std;
int main() {
MyClass test;
stdio_init_all();
}
The Pi Pico repeating_timer API allows you to pass a user_data argument when adding a timer (the third parameter to add_repeating_timer_us).
This value is later returned in the user_data member of the struct repeating_timer passed to your callback.
This can be anything you want, e.g. in your case, a pointer to MyClass, so long as you cast it to and from void *.
I wrote down an example code to try to replicate the error I am getting in a school project about the scope of an object:
In file: classTest.cpp
#include "headerone.h"
#include "headertwo.h"
#include <iostream>
using namespace std;
int main() {
ClassOne* pntrObj1 = new ClassOne;
ClassTwo* pntrObj2 = new ClassTwo;
pntrObj1->testClassOne();
return 0;
}
In file: headerone.h
#ifndef HEADERONE_H
#define HEADERONE_H
#include "headertwo.h"
#include <iostream>
using namespace std;
class ClassOne {
public:
void testClassOne() {
cout << "One Worked\n";
pntrObj2->testClassTwo();
}
};
#endif
In file: headertwo.h
#ifndef HEADERTWO_H
#define HEADERTWO_H
#include <iostream>
using namespace std;
class ClassTwo {
public:
void testClassTwo() {
cout << "Two Worked";
}
};
#endif
To be clear, the error is: pntrObj2 was not declared in this scope. The error comes from the file headerone.h
If I had to guess, I need to somehow pass the reference but I am not sure where to start for that. Any help is appreciated.
The variable pntrObj2 is only visible inside the scope in which it was declared, in this case your function main(). In other words, only code inside the curly braces of main() would be able to use the name pntrObj2 to reference that variable. However you can pass that value to other pieces of code by making it the argument of a function call.
So maybe what you want to do is add an argument to the testClassOne() method, so you can pass in the value of pntrObj2. So pntrObj1->testClassOne(); would become pntrObj1->testClassOne(pntrObj2);, and where you define testClassOne you can add a corresponding parameter. I'll let you figure this out so as to not completely do your homework for you :)
Here you include your file a lot of time and in testClassOne function, you do not declare pntrObj2
use
void testClassOne() {
cout << "One Worked\n";
ClassTwo* pntrObj2 = new ClassTwo()
pntrObj2->testClassTwo();
}
insteed of
void testClassOne() {
cout << "One Worked\n";
pntrObj2->testClassTwo();
}
I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.
I have the below code that compiles and executes without error, but the line that should be printed in the menu() function is never printed.
Menu.cpp
#include "stdio.h"
#include "Menu.hpp"
#include <iostream>
using namespace std;
namespace View
{
void Menu::startMenu()
{
cout << "2\n";
}
}
Menu.hpp
#ifndef MENU_H //"Header guard"
#define MENU_H
namespace View
{
class Menu
{
void startMenu();
};
}
#endif
I wrote a simple test to call the menu function, if it works correctly the output should be
1
2
3
but the 2 is never printed.
MenuTest.cpp
#include "Menu.hpp"
#include "stdio.h"
#include <iostream>
using namespace std;
int main()
{
cout << "1\n";
View::Menu startMenu();
cout << "3\n";
}
Can someone see what's going on here?
View::Menu startMenu();
Declares a function which returns View::Menu type, which is also known as most vexing parse
To initialize an object and call it's member function, you should do:
View::Menu menu;
menu.startMenu();
BTW, you need to make startMenu() function public:
class Menu
{
public: //<-----
void startMenu();
};
See live sample.
help this helps.
Because you declare function "startMenu()", that is returns type "View:Menu"
But you don't call function startMenu().
Try make following code:
View::Menu obj;
obj.startMenu();
PS. And make startMenu() as public:
class Menu
{
public:
void startMenu();
};
When substracting the brckets of View::Menu startMenu();, the code
View::Menu startMenu;
is a object definition of View::Menu, which does not call the function Menu::startMenu(). And that is when "cout << "2\n";" is not executed. To call further Menu::startMenu():
startMenu.startMenu();
Please excuse me but I didn't know to give a name to the title in a short way.
Why do I need to declare an overloaded operator inside the header to make it work in this example:
HEAD.H
#pragma once
namespace test {
class A {
public:
A() : x(0) {}
int x;
};
A& operator++(A& obj); //this is my question
}
HEAD.CPP
#include "head.h"
namespace test {
A& operator++(A& obj) {
++obj.x;
return obj;
}
}
MAIN.CPP
#include <iostream>
#include "head.h"
using namespace std;
using namespace test;
int main() {
A object;
++object; //this won't work if we delete declaration in a header
return 0;
}
operator++ is defined and declared in a namespace inside "head.cpp" so why do I need to declare it one more time in a header?
Thank you.
The CPP files are compiled independently of each other, and they only see the header files they've included (which are in fact textually added to the source code of the CPP before compilation). As such you'll use the header file to inform the compiler that a function exists with that signature (be it an operator overload).
Then the output from your CPP files is put together by the linker, which is when you'd find out if for instance you had declared a function in a header file but never taken the trouble to implement it.
Simple example with namespaces:
#include <iostream>
namespace test{
int f() { return 42; }
int g() { return -1; }
}
namespace other{
int f() { return 1024; }
}
using namespace other;
int main(){
//error: 'g' was not declared in this scope
//std::cout << g() << std::endl;
std::cout << test::f() << std::endl; //42
std::cout << f() << std::endl; //1024
return 0;
}