How to build a binary from multiple c++ files? - c++

I have started learning C++, and have gotten stuck when working with multiple files. To practice basic classes, I wrote three different files,
working.cpp
word.cpp
word.h
word.cpp:
#include <iostream>
#include "word.h"
using namespace std;
class word{
public:
char *word;
void createWord(char *str)
{
word = str;
}
void print_word(void)
{
cout<<word<<endl;
}
char * getWord()
{
return word;
}
}
working.cpp
#include <iostream>
#include "word.h"
void printWord(word);
using namespace std;
int main()
{
word one;
one.createWord("one");
printWord(one);
}
void printWord(word a)
{
cout<<a.getWord()<<endl;
}
word.h
class word;
These are three different files, so I am not sure how to compile them. What I have tried is
g++ working.cpp word.cpp
However, the compiler doesn't recognize word as a class, and gives me the following errors
working.cpp: In function 'int main()':
working.cpp:7:7: error: aggregate 'word one' has incomplete type and cannot be defined
working.cpp:7:12: error: aggregate 'word two' has incomplete type and cannot be defined
working.cpp:7:17: error: aggregate 'word three' has incomplete type and cannot be defined
working.cpp: In function 'void printWord(word)':
working.cpp:19:6: error: 'aha' has incomplete type
In file included from working.cpp:2:0:
word.h:2:7: error: forward declaration of 'class word'
word.cpp:25:1: error: expected ';' after class definition
What am I doing wrong while compiling?

You need to include more of the definition of word in the header file. Something like this:
class word
{
public:
char *word;
void createWord(char *str);
void print_word(void);
char * getWord();
};
Then, change word.cpp to just have the implementations:
void word::createWord(char *str)
{
word = str;
}
void word::print_word(void)
{
cout<<word<<endl;
}
char * word::getWord()
{
return word;
}
compile and link!
You need to have more of the word class in the header so that your other translation unit can know how big the class is (to reserve enough space for the instance you're creating) as well as to know the names of the methods you want to call.

Just mentioning the class name in the header file (a so-called forward declaration) is not enough; you need a complete class declaration (which declares all the fields and functions of the class):
class word {
public:
char *word;
void createWord(char *str);
void print_word(void);
char * getWord();
};

There is no actual declaration of class word in word.h
word.h:2:7: error: forward declaration of 'class word'
I would advise you to read Bjarne Stroustrup's brilliant book "The C++ Programming Language" to get started.

Related

Accessing nested class in C++

I came across this question in an online test that I was taking. The task is to alter this program to get rid of compilation errors.
#include<iostream>
#include<iomanip>
class Vehicle
{
public:
static Car* createCar()
{
return new Car;
}
class Car
{
public:
string name;
};
private:
int seats;
};
void useVehicle()
{
Vehicle::Car *c = Vehicle::createCar();
c->name = "BMW";
}
int main(int argc, char *argv[])
{
useVehicle();
return 0;
}
The compilations errors are like:
error: ‘Car’ does not name a type
error: ‘string’ does not name a type
In function void useVehicle():
error: ‘createCar’ is not a member of ‘Vehicle’
How do I get it right ? I tried few things but could not resolve these errors.
error: ‘Car’ does not name a type
At the point of
static Car* createCar()
Car is not yet known. Move the definition of class Car above the function
error: ‘string’ does not name a type In function ‘void useVehicle()’:
#include <string>
also use std:: to qualify string
error: ‘createCar’ is not a member of ‘Vehicle’
This error will disappear once you fix the other two issues. The compiler wasn't able to parse the function declaration because it didn't know what its return type was.

C++ Class Methods Declaration Returns Error

I have recently started working with C++ classes and had just started when I reached an error. I have a "resource.h" file that contains the class definition of two classes: 'deck' and 'card'. I #included this file in another file, "card.cpp". In the card.cpp file I described all the methods/functions of the 'card' class. However on compilation I am getting the following the errors (fyi I am using the MinGW compiler for command-line):
card.cpp:3:29: error: ISO C++ forbids declaration of 'setCard' with no
type [-fp ermissive] card.cpp:3:1: error: prototype for 'int
Card::setCard(char, char)' does not matc h any in class 'Card'
resource.h:9:8: error: candidate is: void Card::setCard(char, char)
The "card.cpp" file:
#include "resource.h"
Card::setCard(char f, char s) {
face = f;
suit = s;
}
Card::Card (char face, char suit) {
setCard(face, suit);
}
Card::~Card () {}
The "resource.h" file:
typedef unsigned short int UINT;
class Card;
class Deck;
class Card {
public:
Card(char face, char suit);
~Card();
void setCard(char face, char suit);
char getFace() const { return face; }
char getSuit() const { return suit; }
private:
char face;
char suit;
};
class Deck {
public:
Deck();
~Deck();
Card getCard(UINT x);
private:
Card myCards[54];
};
What is causing this issue, and why in the world does the compiler think that "Card::setChard()" is an int
Card::setCard(char f, char s) {
face = f;
suit = s;
}
should be
void Card::setCard(char f, char s) {
face = f;
suit = s;
}
Some hints that helped me get to this amazing conclusion:
C++ forbids declaration of 'setCard' with no type
candidate is: void Card::setCard(char, char)
If you thought this was cryptic, hold on tight for when you get to templates. Compilers have a history of generating great error messages for them.

C++ error: no viable conversion from 'mapped_type' to 'int'

I am trying to implement the a map from the C++ STL as follows:
#include <string>
#include <iostream>
#include <map>
using namespace std;
#include "assembler.h"
// This Class makes use of the Map Template from the Standart Template Library
// All addresses are stored as numerical (Dec) integers
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
int address = 0;
}
void SymbolTable::addEntry(string symbol, int address) {
symbolTable[symbol] = address;
address++;
}
// Returns true if symbolTable already contains symbol
bool SymbolTable::contains(string symbol) {
if (symbolTable.find(symbol) == symbolTable.end()) { return true; }
else { return false; }
}
int SymbolTable::getAddress(string symbol) {
return symbolTable[symbol];
}
I try to compile this with
c++ *.cpp -0 assembler.out
and I get the following error message:
symboltable.cpp:57:9: error: no viable conversion from 'mapped_type' (aka 'std::basic_string<char>') to 'int'
return symbolTable[symbol];
^~~~~~~~~~~~~~~~~~~
1 error generated.
I have searched for this error online and all I get is bug reports relating to the STL and I cannot figure out if those reports are the same problem I am having and if so how to get around it. Am I doing something wrong?
I have tried (probably stupidly) to typecast the offending line as
return (int) symbolTable[symbol];
Thank you for any help.
My header file declares the class as:
class SymbolTable {
public:
SymbolTable();
void addEntry(string, int);
bool contains(string);
int getAddress(string);
private:
map <string, string> symbolTable;
int address;
};
This:
SymbolTable::SymbolTable() { // Constructor
map <string, int> symbolTable;
^
^
is a function-local variable, not a member variable. It is not the same as the symbolTable that you're accessing in e.g. getAddress, which presumably is a member variable. You haven't shown the class body, but my guess is that it's defined differently.

Getting compile errors with Copliens 1994 counted pointer example code

OK, I am reading Copliens C++ Idioms book, and trying to run the handle/body examples in the book. After typing in the code, I am getting compile errors:
Here is the code the String and StringRep classes.
#ifndef _STRINGREP_H_
#define _STRINGREP_H_
#include <stdio.h>
#include <string.h>
class String;
class StringRep {
friend class String;
public:
StringRep() {*(rep = new char[1])='\0';}
StringRep(const StringRep& s) {
::strcpy(rep=new char[::strlen(s.rep)+1], s.rep);
}
~StringRep() { delete [] rep;}
StringRep(const char* s) {
::strcpy(rep=new char[::strlen(s)+1], s);
}
String operator+(const String& s) const {
char *buf = new char[s->length() + length() + 1];
::strcpy(buf, rep);
::strcat (buf, s->rep);
String retval(&buf);
return retval;
}
int length() const { return ::strlen(rep); }
void print() const {::printf("%s\n", rep); }
private:
StringRep(char ** const r) {
rep = *r;
*r = 0;
count = 1;
};
char *rep;
int count;
};
#endif
#ifndef _STRING_H_
#define _STRING_H_
#include <stdio.h>
#include <string.h>
#include "StringRep.h"
class String {
friend class StringRep;
public:
String operator+(const String& s) const {return *p + s;}
StringRep* operator->() const {return p;}
String() {
(p = new StringRep())->count = 1;
}
String (const String &s) { (p=s.p)->count++;}
String(const char* s) {
(p = new StringRep(s))->count = 1;
}
String operator=(const String& s) {
if (--p->count <=0) delete p;
(p = s.p)->count++;
return *this;
}
~String() { if (--p->count <= 0) delete p;; }
private:
String(char **r) {
p = new StringRep(r);
}
StringRep *p;
};
#endif
And a main.cc
#include <stdio.h>
#include <string.h>
#include "StringRep.h"
#include "String.h"
int main() {
String a("abcd"), b("efgh");
printf("a is "); a->print();
printf("b is "); b->print();
printf("concat of a+b is "); (a+b)->print();
return 0;
}
Compile errors;
GNU C++ version 4.1.2 20080704 (Red Hat 4.1.2-44) (x86_64-redhat-linux)
compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-44).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 2d02d8750f9b337bb19a7dd5b4e2167e
StringRep.h: In member function 'String StringRep::operator+(const String&) const':
StringRep.h:21: error: return type 'struct String' is incomplete
StringRep.h:22: error: base operand of '->' has non-pointer type 'const String'
StringRep.h:24: error: base operand of '->' has non-pointer type 'const String'
StringRep.h:25: error: variable 'String retval' has initializer but incomplete type
String.h: In member function 'String String::operator+(const String&) const':
String.h:13: error: conversion from 'void' to non-scalar type 'String' requested
I figure that I cannot use String class until it is completely defines. Changing the
signature of the function to
String& operator+(const String& s) const {...
solves the first error, but causes the same error to show up where I am creating a
new String object
String retval(&buf);
I realize that the book I have is the 1994 reprint that I picked up. So can someone either point me newer code (if C++ coding style has changed) or point out how to fix this?
Thanks
You got a circular reference since StringRep needs to know the full definition of String to construct it in operator+. I advice to not put everything in header files, but just the declarations of the member functions and put the definitions in the .cpp (or .cc) file. That should fix it. That is also how code should be split if the class itself and / or the functions are not a template.
You need to pull the definition and declaration of everything apart. If you really want to put them in the header file, you can do so as follows:
<forward declarations, required includes>
<class itself, no functions defined>
<includes for forward declarations that are needed for function bodies>
<function bodies>
That will always work.
You can't include the String header file in the StringRep header file when you also include the StringRep header file in the String header file. This causes a circular inclusion and will prevent your code from compiling.
Think of it as:
StringRep.h loads String.h
which loads StringRep.h
which loads String.h
which loads StringRep.h
which loads ...
You can avoid this by moving the function declarations into the .cpp files and simply forward declaring the String class inside of StringRep.h (which you already do!). It won't actually try and resolve the reference to the String class until it actually compiles the code, by which time the circular inclusion issue will have been avoided.

'Scanner' does not name a type error in g++

I'm trying to compile code in g++ and I get the following errors:
In file included from scanner.hpp:8,
from scanner.cpp:5:
parser.hpp:14: error: ‘Scanner’ does not name a type
parser.hpp:15: error: ‘Token’ does not name a type
Here's my g++ command:
g++ parser.cpp scanner.cpp -Wall
Here's parser.hpp:
#ifndef PARSER_HPP
#define PARSER_HPP
#include <string>
#include <map>
#include "scanner.hpp"
using std::string;
class Parser
{
// Member Variables
private:
Scanner lex; // Lexical analyzer
Token look; // tracks the current lookahead token
// Member Functions
<some function declarations>
};
#endif
and here's scanner.hpp:
#ifndef SCANNER_HPP
#define SCANNER_HPP
#include <iostream>
#include <cctype>
#include <string>
#include <map>
#include "parser.hpp"
using std::string;
using std::map;
enum
{
// reserved words
BOOL, ELSE, IF, TRUE, WHILE, DO, FALSE, INT, VOID,
// punctuation and operators
LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, SEMI, COMMA, PLUS, MINUS, TIMES,
DIV, MOD, AND, OR, NOT, IS, ADDR, EQ, NE, LT, GT, LE, GE,
// symbolic constants
NUM, ID, ENDFILE, ERROR
};
class Token
{
public:
int tag;
int value;
string lexeme;
Token() {tag = 0;}
Token(int t) {tag = t;}
};
class Num : public Token
{
public:
Num(int v) {tag = NUM; value = v;}
};
class Word : public Token
{
public:
Word() {tag = 0; lexeme = "default";}
Word(int t, string l) {tag = t; lexeme = l;}
};
class Scanner
{
private:
int line; // which line the compiler is currently on
int depth; // how deep in the parse tree the compiler is
map<string,Word> words; // list of reserved words and used identifiers
// Member Functions
public:
Scanner();
Token scan();
string printTag(int);
friend class Parser;
};
#endif
anyone see the problem? I feel like I'm missing something incredibly obvious.
parser.hpp incluser scanner.hpp and vice versa.
So one file evalated before the other.
You can use a forward declaration like
class Scanner;
or reorginaze your headers
You are including Scanner.hpp in Parser.hpp and you are also including Parser.hpp in Scanner.hpp.
If you include Scanner.hpp in your source file then the definition of the Parser class will appear before the definition of the Scanner class and you will get the error you are seeing.
Resolve the circular dependency and your problem will go away (headers should never circularly depend on each other for types).
You have circular #include reference: one header file includes another and vice versa. You need to break this loop somehow.