To start, I'm very new to c++ and Linux, so if you could keep that in mind when you respond that would be great :)
I am trying to create a class in one file, that I will implement in the header of another file when I make an instance of that class.
My class file is
#include <iostream>
int main(){
class Box
{
public:
int _width;
int _length;
int _height;
};
}
I saved this as boxclass.h, but I did NOT compile it. I read somewhere that when I add this to my header file, I should just save it as a text file.
My other file, which I tried to include my box class in, is this:
#include <iostream>
#include "/home/cole/cpp/boxclass.h"
using namespace std;
int main()
{
Box outer{3,4,5};
Box inner{1,2,3};
Box newB = outer-inner;
cout << newB << endl;
}
When I try to compile this, I get these errors repeated many times with many different values
/home/cole/cpp/Boxclass.h:442:864: warning: null character(s) ignored
/home/cole/cpp/Boxclass.h:442:1: error: stray ‘\1’ in program
Can anyone explain to me whats going on?
You have two definitions for the main() {} function. That's not compliant for any c++ compiled code.
Further you have a local declaration of your class here:
int main(){
class Box
{
public:
int _width;
int _length;
int _height;
};
}
You don't want this, but an out of scope declaration of class Box appearing in a separate header file.
I'd suppose what yo want is
#include <iostream>
class Box {
public:
int _width;
int _length;
int _height;
};
int main(){
}
Related
So I have two classes - Dvd and DvdGroup. DvdGroup basically manages an array of dvds and provide manipulative member functions for that class. The problem is whenever I try to compile DvdGroup.cc using the command 'g++ -c Dvd.Group.cc', I get a bunch of errors all related to not having 'Dvd' declared and I'm not sure why.
Here are some errors below:
DvdGroup.h:14:12: error: ‘Dvd’ has not been declared void add(Dvd*);
DvdGroup.h:18:3: error: ‘Dvd’ does not name a type Dvd* dvdCollection[MAX_DVDS];
DvdGroup.cc: In copy constructor ‘DvdGroup::DvdGroup(DvdGroup&)’:
DvdGroup.cc:15:6: error: ‘Dvd’ was not declared in this scope for(Dvd d: dvds){
I feel like I'm missing something and they could all be fixed by one solution because they all involve having the Dvd class undeclared but I can't seem to figure out what. I was wondering if anyone could tell me what I'm doing wrong? I would really appreciate any help with fixing this.
DvdGroup.cc:
#include <iostream>
using namespace std;
#include "DvdGroup.h"
DvdGroup::DvdGroup(int n){
numDvds = n;
}
DvdGroup::DvdGroup(DvdGroup& dvds){
numDvds = dvds.numDvds;
for(Dvd d: dvds){
Dvd newDvd = Dvd;
}
}
DvdGroup::~DvdGroup(){
//code
}
void DvdGroup::add(Dvd* d){
//code
}
DvdGroup.h:
#ifndef DVDGROUP_H
#define DVDGROUP_H
#define MAX_DVDS 15
#include <string>
using namespace std;
class DvdGroup
{
public:
DvdGroup(int);
DvdGroup(DvdGroup&);
~DvdGroup();
void add(Dvd*);
private:
Dvd* dvdCollection[MAX_DVDS];
int numDvds;
};
#endif
Don't know if the Dvd header file is needed, but here:
Dvd.h:
#ifndef DVD_H
#define DVD_H
#define MAX_DVDS 15
#include <string>
class Dvd{
public:
Dvd(string, int);
void set(string, int);
Dvd(Dvd&);
int getYear();
~Dvd();
void print();
private:
string title;
int year;
};
#endif
What you need to do is to provide Dvd class definition for DvdGroup class. It is needed to know what type of symbol is this. Solution for your problem should be addition of:
#include "Dvd.h"
line to DvdGroup.h file.
Im having trouble with a multi-file setup. Im working in visual studio, and, for whatever reason, my friend function in my class is not being defined in main. Any help would be appreciated, thanks.
BullCow.h:
#pragma once
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
class BullCow {
public:
BullCow();
friend int getWins();
static int Wins;
private:
int Attempts;
};
BullCow.cpp:
#include "stdafx.h"
#include "BullCow.h"
int BullCow::Wins = 0;
int getWins() {
return Wins;
}
BullCowMain.cpp:
#include "stdafx.h"
#include "BullCow.h"
int main()
{
srand(time(NULL));
std::cout << getWins();
return 0;
}
Note: It's an incomplete program, so some code (srand) is not used yet. I just included everything to better help figure out what's wrong.
getWins() needs at least a declaration in the .h file.
Since it's a friend, getWins() is not a member of the class, so it must be declared either directly in BullCowMain.cpp or in some file BullCowMain.cpp includes.
Add this somewhere outside of the class in your header:
int getWins();
Also, inside getwins, the return should be:
return BullCow::Wins;
Thanks #user4581301!
Hey i was trying to use Separating interface from implementation but got error.
Not understanding what's wrong.
Here's my program
here's the error image
#include<iostream>
#include"name.h"
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
name n1(x,y);
n1.getdata(x,y);
n1.showdata();
}
now here's the created header file
#include<iostream>
using namespace std;
class name{
private:
int a,b;
public:
name(int x, int y);
void getdata(int x, int y);
int showdata();
};
& here's the next part of class
#include"name.h"
using namespace std;
name::name(int x, int y)
{
a=0;
b=0;
}
void name::getdata(int x,int y)
{
a=x;
b=y;
}
void name::showdata()
{
cout<<a+b;
}
There are many problems with your code. It looks like the best advice in this situation would be to read a good C++ book.
When this is out of the way, here's the short-list of problems in the severity-descending order:
name::showdata() declaration signature does not match difinitioin: int showdata() vs. void showdata()
header misses an include guard
using namespace in a header is a code smell 99 times out of 100
header does not need to include <iostream>, it would suffice to include it in implementation file, where it's actually used.
By looking at undefined references you are getting, I would also guess that name.cpp is not build.
I fixed some of the mentioned points to just make it build:
Live Demo
I am farily new to C++ and I have been stuck with this problem for a few hours now. I am trying to setup the foundations for a video game related experience calculator, but I can't get past this problem.
main.cpp
#include <iostream>
#include "Log.h"
using namespace std;
int main()
{
Log Logs;
enter code here
struct ChoppableLog Yew;
Logs.initialiseLog(Yew, 60, 175);
return 0;
}
Log.h
#ifndef LOG_H
#define LOG_H
struct ChoppableLog
{
int level;
int xp;
};
class Log
{
public:
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int);
Log();
};
#endif // LOG_H
Log.cpp
#include "Log.h"
#include <iostream>
using namespace std;
Log::Log()
{
}
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}
The error I get is
C:\Users\Murmanox\Documents\C++\C++ Projects\CodeBlocks\Class Files Test\main.cpp|11|undefined reference to `Log::initialiseLog(ChoppableLog&, int, int)'|
I can post more details if necessary.
You have to define Log::initialiseLog with its full name, like so:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{ }
What you are doing is defining a new, free function of the name initialiseLog instead of defining the member function of Log.
This leaves the member function undefined, and, when calling it, your compiler (well, technically linker) will be unable to find it.
The definitions of functions in a header file should specify the scope. In your case, you should define initialiseLog() function in your cpp file as follows:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}
I have written this header file (header1.h):
#ifndef HEADER1_H
#define HEADER1_H
class first ;
//int summ(int a , int b) ;
#endif
and this source files (header1.cpp and main.cpp):
#include <iostream>
#include "header1.h"
using namespace std;
class first
{
public:
int a,b,c;
int sum(int a , int b);
};
int first::sum(int a , int b)
{
return a+b;
}
#include <iostream>
#include "header1.h"
using namespace std;
first one;
int main()
{
int j=one.sum(2,4);
cout << j<< endl;
return 0;
}
But when I run this program in codeblocks , I give this Error :
aggregate 'first one' has incomplete type and cannot be defined .
You can't put the class declaration in the .cpp file. You have to put it in the .h file or else it's not visible to the compiler. When main.cpp is compiled the type "first" is class first;. That's not useful at all because this does not tell anything to the compiler (like what size first is or what operations are valid on this type). Move this chunk:
class first
{
public:
int a,b,c;
int sum(int a , int b);
};
from header1.cpp to header1.h and get rid of class first; in header1.h
You need to declare the whole class in a headerfile (that is included every place the class is actually used). Oterhwise, the compiler won't know how to "find" sum in the class (or how much space it should reserve for the class).
If you're using a main function as well, just define the class at the top and define the main later. It is not necessary to explicitly create a separate header file.