I'm getting this linker error. I know a way around it, but it's bugging me because another part of the project's linking fine and it's designed almost identically.
First, I have namespace LCD. Then I have two separate files, LCDText.h and LCDGraphic.h.
LCDText.h:
//[snip]
void TextDraw(Widget *w);
void TextBarDraw(Widget *w);
void TextHistogramDraw(Widget *w);
void TextIconDraw(Widget *w);
void TextBignumsDraw(Widget *w);
void TextGifDraw(Widget *w);
}; // End namespace
LCDGraphic.h:
//[snip]
void GraphicDraw(Widget *w);
void GraphicIconDraw(Widget *w);
void GraphicBarDraw(Widget *w);
void GraphicHistogramDraw(Widget *w);
void GraphicBignumsDraw(Widget *w);
void GraphicGifDraw(Widget *w);
}; // End namespace
And in WidgetBignums.h I have:
//[snip]
using namespace LCD;
extern void TextBignumsDraw(Widget *w);
extern void GraphicBignumsDraw(Widget *w);
template <class T>
WidgetBignums<T>::WidgetBignums(Generic<T> *v, std::string n, Json::Value *section,
int row, int col) : Widget(n, section, row, col,
WIDGET_TYPE_BIGNUMS | WIDGET_TYPE_RC | WIDGET_TYPE_SPECIAL) {
if( v->GetType() == LCD_TEXT )
Draw = TextBignumsDraw; // Line 66
else if( v->GetType() == LCD_GRAPHIC )
Draw = GraphicBignumsDraw;
else
Draw = NULL;
//[snip]
And I get the following linker error:
LCDControl.o: In function `WidgetBignums':
/home/starlon/Projects/LCDControl/WidgetBignums.h:66: undefined reference to `LCD::TextBignumsDraw(LCD::Widget*)'
Now here's one way to fix it, but I don't like it. I can move LCD::TextBignumsDraw outside of the LCD namespace and it works. Strange enough, the linker sees LCD::GraphicBignumsDraw. Any clues?
Edit: I'm using gcc 4.4.1-2 on Fedora 11. Using SCons to compile.
Edit: Here's WidgetBignums, showing Draw.
template <class T>
class WidgetBignums : public Widget {
Generic<T> *visitor_;
std::vector<char> FB_;
std::vector<char> ch_;
int min_;
int max_;
int update_;
int layer_;
Property *expression_;
Property *expr_min_;
Property *expr_max_;
QTimer *timer_;
void (*Draw)(Widget *);
public:
WidgetBignums(Generic<T> *visitor, std::string name, Json::Value *section, int row, int col);
~WidgetBignums();
void TextScroll() {};
void SetupChars();
void Update();
void Start();
void Stop();
std::vector<char> GetFB() { return FB_; }
std::vector<char> GetCh() { return ch_; }
Generic<T> *GetVisitor() { return visitor_; }
};
Edit: Here's TextBignumsDraw's signature.
//[snip]
void TextBignumsDraw(Widget *w) {
//[snip]
Edit: Incidentally, I'm getting the same error for TextHistogramDraw and TextGifDraw as well. TextIconDraw and the others are fine.
Where is the definition for LCD::TextBignumsDraw()? That's what the linker seems to be complaining about. Not the declaration, but the actual definition of the function.
The fact that when you move the declaration out of namespace LCD things start working indicates that the definition for TextBignumsDraw() is in the global namespace, not the LCD namespace.
This (in some .cpp file):
void TextBignumsDraw(Widget *w) {
// ...
}
Needs to be wrapped in a
namespace LCD {
}
block.
Try dropping the "using namespace LCD", and change that line 66 to:
Draw = LCD::TextBignumsDraw;
That's more explicit, which may help the linker understand what you're asking for.
Besides, you should never say "using namespace Anything" in a header file. It hoists everything in that namespace out into the global space for every user of that header. That almost completely destroys the value of having a namespace in the first place. You should hoist things out like this in the narrowest scope that's practical. Sometimes I put "using namespace foo" at the top of a single function, for instance, if that's the only user of the bits in the namespace within a given .cpp file.
Related
I've done this so many times, yet the reason why Visual Studio is complaining about this escapes me.
Manipulator.cpp:
#include "Manipulator.h"
Manipulator::Manipulator() {}
Manipulator::~Manipulator() {}
void proc(std::string p, int f, std::string c)
{
// switch-case p to c based on f:
return;
}
Manipulator.h: (void -proc- has a curly underscore, and that's what's driving me up the wall.)
#ifndef MANIPULATOR_H
#define MANIPULATOR_H
#include <string>
class Manipulator
{
private:
protected:
public:
Manipulator() ;
~Manipulator() ;
void proc(std::string, int, std::string);
// function definition for 'proc' not found.
};
#endif MANIPULATOR_H
main.cpp
#include "Manipulator.h"
...
int main()
{
...
Manipulator m;
...
m.proc(opdBMP, fxn, newBMP);
return 0;
}
What is it that VS wants so that I can get a move on? It is telling me that there are two linker errors: LNK2019 and LNK1120 (unresolved external). (I used to keep track of these kinds of errors but lost the file as a log with these.)
The compiler is correct in complaining, because the definition should be
void Manipulator::proc(std::string p, int f, std::string c) {
...
}
You just defined a free function instead of a member of Manipulator.
I am attempting to program an Atmel SAM D21 microcontroller using C++ in Atmel Studio. I'm trying to create periodic hardware interrupts using one of the on-chip timers.
I created Timer4 class to set up the timer from main.cpp. I tried to create a Timer4 instance called MyTimer4 in the main function, but it says
'Timer4' was not declared in this scope
'MyTimer4' was not declared in this scope
I've seen many similar discussions pointing to incorrect/circular #includes. But, I don't seem to see the same problem on my own. Any ideas?
Main.cpp
#include "timerSAMD21.h"
#include "sam.h"
void SampleADC(void)
{
}
int main(void)
{
SystemInit();
Timer4 MyTimer4;
MyTimer4.setRate(1000);
MyTimer4.onEvent(SampleADC);
MyTimer4.start;
}
timerSAMD21.h
#ifdef TIMERSAMD21_H
#define TIMERSAMD21_H
#include "tc.h"
#include "tc4.h"
#include "gclk.h"
typedef void (*voidFuncPtr)(void);
class Timer4
{
public:
Timer4() {};
void setRate(int frequency);
void start(void);
void end(void);
void onEvent(voidFuncPtr funcOnEvent);
private:
void configure(int frequency);
void enable(void);
void disable(void);
void reset(void);
};
#endif
timerSAMD21.cpp
#include "timerSAMD21.h"
voidFuncPtr callback = NULL;
void Timer4::setRate(int frequency) {
configure(frequency);
}
void Timer4::start(void) {
enable();
}
void Timer4::end(void) {
disable();
reset();
}
void Timer4::configure(int frequency) {
//Configuration code here. Removed for Stack Overflow.
}
void Timer4::enable(void){
REG_TC4_CTRLA |= TC_CTRLA_ENABLE; //Enable timer
while (TC4->COUNT8.STATUS.bit.SYNCBUSY);
}
void Timer4::disable(void) {
REG_TC4_CTRLA &= ~TC_CTRLA_ENABLE;
while (TC4->COUNT8.STATUS.bit.SYNCBUSY);
}
void Timer4::reset(void) {
REG_TC4_CTRLA = TC_CTRLA_SWRST;
while (TC4->COUNT8.STATUS.bit.SYNCBUSY);
while (TC4->COUNT8.CTRLA.bit.SWRST);
}
void Timer4::onEvent(voidFuncPtr funcOnEvent){
callback = funcOnEvent;
}
#ifdef __cplusplus
extern "C" {
#endif
void IRQHandlerTimer4(void) {
if (callback != NULL)
{
callback();
}
REG_TC4_INTFLAG = TC_INTFLAG_MC0;
}
#ifdef __cplusplus
}
#endif
(Note: Making an answer in order to get this out of the list of unanswered questions. Miles seems to have decided not to answer and I do not consider the problem a typo.)
The way you attempt to prevent the reinclusion of your header is causing it to only make the content of the header visible if the guard-macro happens to be defined already, which it of course never is.
In order to fix this, change the
#ifdef TIMERSAMD21_H
#define TIMERSAMD21_H
into
#ifndef TIMERSAMD21_H
#define TIMERSAMD21_H
This will first keep the header content visible the first time it is included.
It will then define the guard macro, which will prevent the header content from being compiled a second time in the same translation unit, i.e. code file.
Error message:
Link to full program
class AddressBook
{
private:
char firstname[20];
char lastname[20];
char no[15];
class adrs
{
public:
char postal[100];
char pincode[7];
friend void say();
friend void Add();
friend void Edit();
friend void View(int);
}address;
char dob[11];
char email[50];
public:
friend void say();
void sort(AddressBook []);
void NumberSort(AddressBook []);
void Add(void);
void Delete(AddressBook [], int pos);
void Edit();
void LinearSearch(AddressBook [], char a[]);
friend void ViewAll();
void View(int);
void FetchContact();
};
This is the declaration of a class for a contact-book program.
void sort(AddressBook []);
void NumberSort(AddressBook []);
void Delete(AddressBook [], int pos);
void LinearSearch(AddressBook [], char a[]);
These lines in the above declaration shows up as an error in TurboC++ compiler. Can anyone tell me why?
In C++ when an array is passed as an argument, its initial address is passed to formal parameter. With the help of this technique the code can be written as follows
void sort(AddressBook*);
void NumberSort(AddressBook*);
void Delete(AddressBook*, int pos);
void LinearSearch(AddressBook*, char a[]);
I have implemented the same thing for my problem and it just worked.
As you were not able to produce minimal example, I did it for you:
class AddressBook
{
void sort(AddressBook[]);
};
This declaration compiles fine on a modern compiler but not on Turbo C++.
You can call this a compiler bug.
There are two options:
you rethink your program and write it another way
you use a modern C++ compiler
Changing school would be another valuable option.
This question already has an answer here:
g++ error: ‘vec’ does not name a type [duplicate]
(1 answer)
Closed 8 years ago.
Ok so I am trying to map some of my member functions in the .h file this is to be able to use the map when I implement the code. However, after hours I have gotten nowhere so I would like suggestions or if anyone knows how to implement this. For reference these are the errors.
./Assembler.h:51:2: error: C++ requires a type specifier for all declarations
functions["load"] = load;
^~~~~~~~~
./Assembler.h:51:12: error: size of array has non-integer type 'const char [5]'
functions["load"] = load;
^~~~~~
./Assembler.h:51:2: error: duplicate member 'functions'
functions["load"] = load;
^
As for my header file it with the problem coming from the map:
#include <vector>
#include <iostream>
#include <map>
#include <string>
#include <fstream>
using namespace std;
class Assembler {
public:
Assembler(string filename);//Argument will be passed from the os.cpp file
void parse();// Will go through the a file to output the .o file
void load();
void loadi();
void store();
void add();
void addi();
void addc();
void addci();
void sub();
void subi();
void subc();
void subci();
void ander();
void andi();
void xorer();
void xori();
void negate();
void shl();
void shla();
void shr();
void shra();
void compr();
void compri();
void getstat();
void putstat();
void jump();
void jumpl();
void jumpe();
void jumpg();
void call();
void ret();
void read();
void write();
void halt();
void noop();
private:
typedef void (*function)();
map<string, function> functions;
functions["load"] = load;
fstream in, out; //One will be the .s file while the other will be the .o file
string opcode;
int rd, rs, constant, addr, machcode; //Different parts of the instruction
};
Any help or suggestions would be appreciated. Thanks
Only static const integral data members can be initialized within a class. You probably need to move functions["load"] = load; to a function's definition.
And also, you need to change them to:
typedef void (Assembler::*function)();
...
functions["load"] = &Assembler::load;
Within C++ class declaration, you cannot have member initialiser or executable statement, Have this one
functions["load"] = load;
within constructor
Take a look at your class declaration: everything's compiling fine except
functions["load"] = load;
This is an assignment and initializes the functions map with something. That is not allowed in the declaration which is a "contract" (in the case of an interface) or "explanation" of how your class is composed and what methods/members has.
The right spot to put such an initialization is in your constructor's definition (i.e. in the part of the code that actually contains the code of your methods, specifically when the object gets created if you intend to initialize stuff.. i.e. the constructor).
#include <QFile>
#include <QString>
// this is some sort of low-level C function
void lowLevelOpenFD(int fd)
{
qDebug("Opened by fd: %d", fd);
}
// as well as this one
void lowLevelOpenName(const char *name)
{
qDebug("Opened by name: %s", name);
}
// this is a wrapper around low-level functions
template<typename FileId>
void callLowLevelOpen(FileId id);
template<>
void callLowLevelOpen(const QString &fileName)
{
lowLevelOpenName(QFile::encodeName(fileName).constData());
}
template<>
void callLowLevelOpen(int fd)
{
lowLevelOpenFD(fd);
}
// this is the function where the most stuff happens
template<typename FileId>
void openInternal(FileId id)
{
// lots of useful stuff goes here
// now we call one of the two low level functions
callLowLevelOpen(id);
// more useful stuff
}
// this is high-level interface to the "open by name" function
void openFile()
{
QString name = "file";
openInternal(name);
}
// this is high-level interface to the "open by FD" function
void openFile(int fd)
{
openInternal(fd);
}
int main()
{
openFile();
openFile(17);
return 0;
}
The problem is that the example above results in
error LNK2019: unresolved external symbol "void __cdecl callLowLevelOpen<class QString>(class QString)" (??$callLowLevelOpen#VQString####YAXVQString###Z) referenced in function "void __cdecl openInternal<class QString>(class QString)" (??$openInternal#VQString####YAXVQString###Z)
As far as I can see it happens because the compiler instantiates openInternal<QString>() when it's called from the first high-level overload. OK, so I thought and modified the code:
// this is high-level interface to the "open by name" function
void openFile()
{
QString name = "file";
openInternal<const QString&>(name);
}
The same problem. And I thought I told the compiler to instantiate the openInternal<const QString&>, so why it is still complains about <class QString>? I also tried this one:
// this is high-level interface to the "open by name" function
void openFile()
{
QString name = "file";
openInternal<const QString&>(static_cast<const QString&>(name));
}
Now this just looks silly and it still doesn't work. I can't explicitly specialize the openInternal() one because it's too large and the very point of this templated mess is to avoid unnecessary code duplication. I can't just rename the low level functions to turn them into overloaded ones, because they are in a third-party C library. The only thing I can do is to replace the first callLowLevelOpen() specialization with
template<>
void callLowLevelOpen(QString fileName)
{
lowLevelOpenName(QFile::encodeName(fileName).constData());
}
Then it works. There is also virtually zero performance penalty so this is a perfectly valid workaround, but I just want to understand what is going on here.
The code above was just an SSCCE, the real code is there if anyone interested. This particular issue is with the gzopen()/gzdopen(), QuaGzipFilePrivate::open() and QuaGzipFile::open() functions.
Since you actually change the signature, I think you actually don't want to specialize your function template but rather use overloading. Here is complete test program using std::string (I somehow prefer depending only on standard classes if the problem can be reproduced there as well):
#include <string>
template <typename T> void f(T);
// #define BROKEN
#if defined(BROKEN)
template <> void f(std::string const&) {}
#else
void f(std::string const&) {}
#endif
int main()
{
std::string s;
f(s);
}
If you #defined BROKEN in this code it won't work.
The reason for this behavior is that the compiler choose the overload based on the primary template. This will never add the const& part. Once this is done the compiler looks for potential specializations of the chosen overload. Since this will never have deduced the notation used for specialization this isn't picked up.
Why then is the f<std::string const&>(s) not picking up the specialization? For me it is, trying with both gcc and clang.