I have been stuck on this, my teacher doesn't even know what's going on. If someone could please help me that would be greatly appreciated.
I have declared item in the header file in the Line struct. However when calling on it in the Line::display() method, i get an error stating that the variable was not declared in the scope. I have showed my teacher and my peers and no one seems to know of the solutions.
Here is my .h:
//Line.h
#define MAX_CHARS 40
struct Line {
public:
bool set(int n, const char* str);
void display() const;
private:
char item[MAX_CHARS];
int no;
};
And here is my .cpp file.
// Line.cpp
#include "Line.h"
#include <iostream>
using namespace std;
#define MAX_CHARS 40
void Line::display() const {
cout << no << ' ' << item << endl;
}
Any help with this is awesome. Thanks in advance.
If this is your actual code, you're probably getting the header from somewhere else. Try:
#include "C:\\fullPathToHeader\\Line.h"
#include <iostream>
using namespace std;
void Line::display() const {
cout << no << ' ' << item << endl;
}
Also:
don't re-define MAX_CHARS in the cpp file.
use include guards for the header.
To make Line::display const implies you do not need instance variable data.
// Line.cpp
#include "Line.h"
#include <iostream>
using namespace std;
void Line::display()
{
cout << no << ' ' << Line::item << endl;
}
Related
I'm having a problem when using the class files and the compiler error says "error: 'x' was not declared on this code" while it points out the cout, string, and endl. I have already wrote "#include " and "#include " in both header, class, and main file.
(Sorry for my English)
I'm just a beginner and I wanted to know the basics
Added #include and #include in both files
//Main File (main.cpp)
#include <iostream>
#include "test.h"
#include <string>
using namespace std;
int main()
{
test *person = new person("Phroton",14)
person.Display();
return 0;
}
//test.h
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <string>
class test
{
private:
string name;
int age;
public:
void Display(){
cout << "I'm " << name << " and I'm " << age << "years old" << endl;
}
};
#endif // TEST_H
//test.cpp (There is no problem with this file at all)
#include "test.h"
#include <iostream>
#include <string>
test::test(string iname, int iage)
{
name = new string;
age = new int;
*name = iname;
*age = iage;
}
test::~test()
{
delete name;
delete age;
cout << "Info Deleted" << endl;
}
Answering the specific problem you have asked:
This is because you have not specified the namespace cout and endl belong to in the file test.h.
The statement in Display should be:
std::cout << "I'm " << name << " and I'm " << age << "years old" << std::endl;
The alternative to this is the using namespace std declaration but this is considered a bad practice (especially in a header file).
Note:
You do not need using namespace std in main.cpp as you are not using any functions from the std namespace there. Even if you do, use the std::name instead of the using declaration.
Member function definitions are usually present in .cpp files. So you can define the function Display to test.cpp.
Also consider moving away from raw pointers to smart pointers.
I have a compiling error in C++ using classes. I have worked with classes before and have never encountered this error. I have tried adding static before the method ImprtData but that only prompted more errors.
error: invalid use of non-static member function bank.ImprtData;
here is my .cpp
#include "componets.h"
User::User() {
std::cout << "loaded" << std::endl;
}
void User::ImprtData() {
std::cout << "loaded.\n";
}
and here is my .h
#include <sstream>
#include <fstream>
#include <vector>
#include <iostream>
#include <string>
class User {
public:
User();
void write();
void launch_main_menu();
void login();
void ImprtData();
private:
void deposit();
void withdrawl();
std::string account_name;
int account_pin;
float account_balance;
std::string account_user_name;
};
and finally my main
#include "componets.h"
int main() {
std::cout << "Welcome to Bank 111.\n";
User bank;
bank.ImprtData;
return 0;
}
This is essentially a simple typo. Replace
bank.ImprtData;
with
bank.ImprtData();
to call the function. The expression bank.ImprtData is confusing the compiler since it's interpreting it as the address of a function, and issues a diagnostic since the function is not static.
bank.ImprtData; should be bank.ImprtData();
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 am trying to learn C++, however, the parameter to a method I have in my own class is misbehaving. When it uses a dataType of 'int', it works fine with no errors, but when I attempt to change it to a 'string' dataType, the program crashes with this error.
Error 1 error C2061: syntax error : identifier 'string' in temp.h ln
8 col 1
The classes I am using are as follows:
WORKING CODE
TesterClass.cpp // Entry Point
#include "stdafx.h"
#include "Temp.h"
int _tmain(int argc, _TCHAR* argv[])
{
Temp tmp;
tmp.doSomething(7);
return 0;
}
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(int blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(int blah)
{
std::cout << blah;
}
BROKEN CODE
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(string blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(string blah)
{
std::cout << blah;
}
When I adjust the parameter 'blah' to be a string, in both the .h and .cpp file, the problem occurs.
I have looked around, but none of the answers seem to solve my problem. I would greatly love help on this an I am out of ideas. I have tried reinstalling C++, messing with:
using namepace std;
using std::string;
std::string instead of string
etc.
If you know how to solve my problem I would love to hear from you. I am more than happy to provide more information.
C++ performs single-pass compilation, so std::string needs to be declared before you use it at all - including in the header file.
// Temp.h
#pragma once
#include <string>
class Temp
{
public:
Temp();
void doSomething(std::string blah);
};
I would encourage you to be specific in your header files when specifying classes like this, because you might easily come across another library that defines it's own string and then you would run into naming conflicts. Save the using import statements for your cpp files.
πάντα ῥεῖ had the write answer, thankyou!
They said to use std::string when needed, and to also #include <string> in the header file.
I am developing a program in c++ with lots of file io operation. I have defined a static ofstream in a common header so that it is accessible everywhere in the project. The structure of the codes are list as following: all common variable are defined in com.h, test.h and test.cpp are for a class called OPClass, main.cpp carry the main program
COM.H:
#ifndef __CLCOM__
#define __CLCOM__
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
static ofstream out;
static stringstream ss;
#endif
TEST.H:
#ifndef __CL__
#define __CL__
#include <iostream>
#include <fstream>
#include "com.h"
using namespace std;
class OPClass
{
public:
void run(void);
void show(ostream &o) const;
};
#endif
TEST.CPP:
#include "com.h"
#include "test.h"
void OPClass::run(void)
{
out << "Here is run()" << endl;
show(out);
}
void OPClass::show(ostream &o) const
{
o << "hello!" << endl;
}
MAIN.CPP:
#include "com.h"
#include "test.h"
void runmain(void)
{
OPClass op;
out.open("output.txt", ios::out | ios::trunc);
out << endl << "State changed!" << endl;
op.run();
if (out.is_open()) out.close();
}
int main(int argc, char* argv[])
{
runmain();
return 0;
}
As you can see, the static ofstream was named as out and will be called in the main program and in the class. I am using mingw32 and didn't see any problem on compilation or upon running. But seems that only the information in runmain() will be written to the output file. Any other message written to that file in the class never appear in the output file. Why's that and how can I written a common file stream so everywhere in the project can access that? Thanks.
Each compilation unit is getting its own ss and out. Hence there is a different instance of them seen by main.cpp than by test.cpp.
You don't really need static here. To address this, rather than declaring the variables and their allocations in the header file you need to merely prototype them using the extern keyword.
#ifndef __CLCOM__
#define __CLCOM__
#include <sstream>
#include <fstream>
#include <iostream>
// Note: don't put "using" statements in headers
// use fully qualified names instead
extern std::ofstream out;
extern std::stringstream ss;
#endif
Where you actually put your declarations is up to you, just make sure it's only in one place. That could be a com.cpp file or you could stick it in main.cpp if that's appropriate for your project.
std::ofstream out;
std::stringstream ss;
Not that global variables like this are a good idea, anyway...
Preemptive statement: You should accept #HostileFork's answer.
Just as an addendum, an easy way to show what's happening is to print out the address of out whenever you try to use it.
If you add these couple statements:
void OPClass::run(void)
{
cout << "Address of 'out' = " << &out << endl;
out << "Here is run()" << endl;
show(out);
}
And:
void runmain(void)
{
cout << "Address of 'out' = " << &out << endl;
OPClass op;
out.open("output.txt", ios::out | ios::trunc);
out << endl << "State changed!" << endl;
op.run();
if (out.is_open()) out.close();
}
You'll notice that the two print statements for out display two different addresses. This should tell you that you're actually getting two instances of out created as two distinct variables. The methods in your OPClass are trying to write to a completely different output stream. It has to do with the way you're using static in a global context; it doesn't behave like you think it does. In a global context, declaring something static binds it to the local scope of the file it's in.