GCC Debugging/g++/Errors

< GCC Debugging < g++

abstract declarator 'TYPE' used as declaration

struct {  // error, no name
   int bar;
};
#ifndef foo
#define foo
#include <vector>

struct foo {  // error, foo already in use
   std::vector<int> bar;
};

#endif

'VARIABLE' cannot appear in a constant-expression


'VARIABLE' cannot be used as a function

int foo(int baf) { return baf; }
 
int bar(int foo) {
   foo = foo(4);
   return foo; 
}

conversion from 'TYPE' to non-scalar type 'TYPE' requested

class Foo {
public:
   int x;
};
 
class Bar {
public:
   Foo Maz() { return 0; }  // 0 is of type int, not Foo
};

could not convert 'STATEMENT' to 'bool'

// you had: 
foo operator<(const foo & f) const

// instead of:
bool operator<(const foo & f) const
string x = "foo";
if (x) cout << "true" << endl;

declaration of 'FUNCTION' outside of class is not definition

class Foo 
{
public:
   int bar;
   Foo(int x);
}; 
 
Foo::Foo(int x);  // semicolon ';' needs to be removed
{
   bar = x;
}

declaration of 'VARIABLE' shadows a parameter

int foo(int bar)
{
   int bar;
   return bar;
}

'TYPE' does not name a type

ostream & os;  // instead of: std::ostream & os;
#include <iostream>
// missing vector library include
class Foo {
public:      
   std::vector<int> Bar(std::vector<int> FooBar) { return FooBar; }
};
// test.h file
#ifndef TEST_H_
#define TEST_H_
std::string bar;
#endif

// test.cpp file
#include "test.h"
#include <iostream>  // error, needed before test.h
using namespace std;

int main()
{
   cout << bar << endl;
   return 0;
}

expected 'TOKEN' before 'TOKEN' token

const int MAX = 10  // error

int main() {
   string foo;
   cout << foo.size();
   return 0;
}
int foo = 0, bar = 0;
cin foo >> bar;  // should be: cin >> foo >> bar;

expected primary-expression before 'TOKEN'

expected primary-expression before 'int'
int sum(int x, int y) { return (x + y); }

int main() {
   int a = 4, b = 5;
   sum(a, int b); // int is the problem causer
   return 0;
}

expected unqualified-id before

class Foo() {
public:
   int x;
};
int foo = 3, bar = 2;
if (foo > bar)  // error, no "{"
   cout << foo << endl;
}

incompatible types in assignment of 'TYPE' to 'TYPE'

char bar[10];
const char *foo = "ppp";
bar = *foo;  // error

// possible fix, use strcpy from the cstring header:
char bar[10];
const char *foo = "ppp";
strcpy(bar, foo);
char foo[2][3];
foo[1] = ' '; // error, need both dimensions, eg: foo[1][0] = ' ';

invalid conversion from 'TYPE' to 'TYPE'

char foo = 'f';
char bar[] = "bar";
if (strcmp(foo, bar) != 0)
   cout << "Correct answer!";
// strcmp was expecting 2 character pointers, foo doesn't qualify

invalid operands of types 'TYPE' and 'TYPE' to binary 'FUNCTION'

// attempting to combine two C-strings
cout << "abc" + "def";

// possible fix: convert 1 argument to a string type
cout << "abc" + string("def");

invalid use of template-name

invalid use of template-name 'TEMPLATE' without an argument list
template <class T> class Foo {
private:
   int x;
public:
   Foo();
};

template<class T> Foo::Foo() { x = 0; }  // error, should be: Foo<T>::Foo()

is not a member of

example: 'cout' is not a member of 'std'
// test.cpp
// file is missing iostream include directive
int main() {
   std::cout << "hello, world!\n";
   return 0;
}

'TYPE' is not a type

void foo(int x, vector y);

'CLASS_MEMBER' is private within this context

class FooBar {
private: int bar;
public: friend void foo(FooBar & f);
};
void fooo(FooBar & f) {  // error
   f.bar = 0;
}
class Foo {
private: Foo() {}
public: Foo(int Num) {}
};
class Bar : public Foo {
public: Bar() {}
// Bar() implicitly accesses Foo's private constructor
};
solution 1: use an initializer list to bypass implicit initialization
solution 2: make the accessed base class member protected instead of private
class Foo {
private: char mrStr[5];
public: Foo(const char *s = "blah") { strcpy(mrStr, s); }
};

class Bar {
private:
   int mrNum;
   Foo aFoo;
public:
   Bar(int n, const Foo &f);
};

// error, attempting to use the Foo class constructor by accessing private data:
Bar::Bar(int n, const Foo &f) : aFoo(f.mrStr) {  // mrStr is private
   mrNum = n;
}

possible fix, assign the whole object rather than part of it:

Bar::Bar(int n, const Foo &f) : aFoo(f) {
   mrNum = n;
}

ISO C++ forbids declaration of 'FUNCTION' with no type

Foo() { return 0: }
// should be: int Foo() { return 0: }

multiple definitions of

eg: multiple definition of `main'

'CLASS FUNCTION(ARGUMENTS)' must have an argument of class or enumerated type

eg: CLASS_NAME FUNCTION_NAME(CLASS_NAME OBJECT_NAME, ARGUMENTS)
class Foo {
public:
   friend int operator+(int x, int y);
};

new types may not be defined in a return type

semicolon missing after definition of 'CLASS'
ISO C++ forbids defining types within return type
class Foo {
public:
   int x;
}  // Error

no match for call to 'FUNCTION'

string bar() {
   string foo = "blah";
   return foo;
}

int main() {
   string bar;
   bar = bar();  // error, "bar()" was hidden by string initialization
   return 0;
}

no matching function for call to 'FUNCTION'

// broken code
ifstream in;
string MrString = "file.txt";
in.open(MrString);

// solution: convert the string to a C-string
ifstream in;
string MrString = "file.txt";
in.open(MrString.c_str());

non-constant 'VARIABLE' cannot be used as template argument

template <class T, int num>
class Bar {
private:
   T Foo[num];
};

int main() {
   int woz = 8;
   Bar<double, woz> Zed;  // error, woz is not a constant
   return 0;
}

non-member function 'FUNCTION' cannot have cv-qualifier

error: non-member function 'int Foo()' cannot have cv-qualifier
cv = constant / volatile
template<class Type>
class Foo {
private:
   int stuff;
public:
   int bar() const;
};

template<class Type>
int Foo::bar() const {  // error
   return stuff;
}

possible fix:

template<class Type>
int Foo<Type>::bar() const {
   return stuff;
}

passing 'const OBJECT' as 'this' argument of 'FUNCTION' discards qualifiers

request for member 'NAME' in 'NAME', which is of non-class type 'CLASS'

request for member 'NAME' in 'NAME', which is of non-aggregate type 'TYPE'
class Foo {
public:
   int x;
   Foo(int num = 0) { x = num; }
   void newX(int num);
};

void Foo::newX(int num) {
   *this.newX(num);  // error, need (*this).newX or this->newX
}

statement cannot resolve address of overloaded function

class Foo {
public:
   int Bar() { return 0; }
};

int main() {
   Foo x;
   x.Bar;  // error
   return 0;
}

two or more data types in declaration of 'NAME'

int char sum(int x, int y);  // int char

<GOBBLEDEGOOK> undefined reference to <GOBBLEDEGOOK>

// header file
void foo();
void bar();
void baz();

// implementation file, bar definition is missing
void foo() { cout << "foo\n"; }
void baz() { cout << "baz\n"; }

'NAME' was not declared in this scope

lonh wait; 

// instead of:
long wait;
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.