Why is my class object causing undeclared identifier errors? - c++

I am writing a program on Microsoft Visual C++ 2010 Express and am having an issue with my class object. I keep getting undeclared identifier and missing ';' before identifier errors and I'm not sure what is causing the problem.
I've checked my headers for any mistakes and have made several changes to see if it would fix the problem with no luck.
// RightJust class declaration that right justifies the numbers.
#ifdef RIGHTJUST_H_
#define RIGHTJUST_H_
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace System;
using namespace std;
class RightJust
{
private:
int x, totalWH1, totalWH2, totalWH3, totalWH4;
int itemT1, itemT2, itemT3, itemT4, itemT5; // Holds item totals
string WH1, WH2, WH3, WH4;
int num[4][5]; // Array containing data
public:
RightJust();
int warehouseLen();
void rightJustZero();
void rightJustLess();
};
// This program asks to user to choose a report type and displays
// the report type chosen.
#include "stdafx.h"
#include <iostream>
#include "RightJust.h"
#include <string>
#include <cstring>
using namespace System;
using namespace std;
int main
{
RightJust type;
if(reportType == 1)
{
type.rightJustZero();
}
else if(reportType == 2)
{
type.rightJustLess();
}
}
I just want a solution to get rid of all the error messages to see if my program is working how I needed it to work.
Test3.cpp
1>Test3.cpp(24): error C2065: 'RightJust' : undeclared identifier
1>Test3.cpp(24): error C2146: syntax error : missing ';' before identifier 'type'
1>Test3.cpp(24): error C2065: 'type' : undeclared identifier
1>Test3.cpp(27): error C2065: 'type' : undeclared identifier
1>Test3.cpp(27): error C2228: left of '.rightJustZero' must have class/struct/union
1> type is ''unknown-type''
1>Test3.cpp(31): error C2065: 'type' : undeclared identifier
1>Test3.cpp(31): error C2228: left of '.rightJustLess' must have class/struct/union
1> type is ''unknown-type''

The include guard in your header is wrong. It should be
#ifndef RIGHTJUST_H_
Using #ifdef RIGHTJUST_H_ will skip the code if the symbol is not defined.
You should not have #include "stdafx.h" in your .h files. And since you seem to be using Visual Studio, you can just use #pragma once instead of the include guard.

Related

Boost thread.hpp include error

I am able to include boost libraries in a simple sample project without any problem and it works fine. But as soon as I include #include <boost/thread.hpp> in one of my other project which is already using some other libraries, I am getting the following compilation errors:
1>ClCompile:
1> ThreadManager.cpp
1>C:\Boost\include\boost-1_63\boost/container/container_fwd.hpp(302): warning C4003: not enough actual parameters for macro 'dummy'
1>C:\Boost\include\boost-1_63\boost/container/container_fwd.hpp(302): error C2976: 'atl::Dummy' : too few template arguments
1>C:\program files\adaptive vision\adaptive vision studio 4.3 professional\sdk\include\ATL/Dummy.h(17) : see declaration of 'atl::Dummy'
1>C:\Boost\include\boost-1_63\boost/container/container_fwd.hpp(302): error C3484: syntax error: expected '->' before the return type
1>C:\Boost\include\boost-1_63\boost/container/container_fwd.hpp(302): error C2061: syntax error : identifier 'Get'
1>C:\Boost\include\boost-1_63\boost/container/container_fwd.hpp(302): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>
1>Build FAILED.
PS: If I include #include <boost/chrono.hpp> instead of #include <boost/thread.hpp> then there is no error.
PSS: I am using Windows, Visual Studio 2010 (that's why Boost)
Sample Code:
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
//Header for boost multithreading
//#include <boost/chrono.hpp>
#include <boost/thread.hpp>
namespace avs //Adaptive Vision
{
class MyProgram : public UserFilter
{
int Invoke()
{
//CODE
}
};
}

When im moving a class to a header file i get an error C++

Something annoying is going on here and I hope the community can help me :). My program is working correctly when I have my class in the cpp file. When I move my class code into a header file the program throws errors. Please instruct me. Thank you!
.cpp file
#include <iostream>
#include <string>
#include "CSquare.h"
using namespace std;
int main()
{
CSquare alo(1,"name");
}
CSquare.h
#pragma once
class CSquare
{
private:
int squareCode;
string squareName;
public:
CSquare(int, string);
void setCode(int);
void setName(string);
};
CSquare::CSquare(int inputSquareCode, string inputSquareName)
{
setCode(inputSquareCode);
setName(inputSquareName);
}
void CSquare::setCode(int inputSquareCode)
{
squareCode = inputSquareCode;
}
void CSquare::setName(string inputSquareName)
{
squareName = inputSquareName;
}
I have also tried moving the #include string in both files but still, nothing seems to fix the problem :/
error C3646: 'squareName': unknown override specifier error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2061: syntax error: identifier 'string' C2061: syntax
error: identifier 'string' – Mash 16 mins ago
error C2061: syntax error: identifier 'string'
error C2065: 'inputSquareName': undeclared identifier
error C2065: 'string': undeclared identifier error C2146: syntax error: missing ')' before identifier 'inputSquareName'
error C2143: syntax
error: missing ';' before '{'
error C2447: '{': missing function header (old-style formal list?)
error C2661: 'CSquare::CSquare': no overloaded function takes 2 arguments –
CSquare.h lacks the definition for the type string.
Solution: Perhaps you intended to use std::string. In that case you must include <string> in CSquare.h and use the scope resolution operator to refer to the string declared in the std namespace. See the first sentence of this paragraph for an example.
CSquare.h contains definitions to non-inline functions. If the header is included in more than one translation unit, then you violate the one definition rule.
In the entire program, an object or non-inline function cannot have more than one definition
Solution: Either define the functions in a single source file, or declare the functions inline.

Visual C++ 2013 & error C2065: 'complex' : undeclared identifier & error C2059: syntax error : ','

#include "stdafx.h"
#include "stdio.h"
#include <iostream>
#include "math.h"
#include <complex>
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include <io.h>
void multiple(complex**,complex*,complex*);
void solve(complex**, complex*, complex*, int);
As I typed the above-mentioned code, we receive the following errors:
error C2065: 'complex' : undeclared identifier
error C2059: syntax error : ','
Many thanks for your time,
The class is located in the namespace std as well as all other C++ standard library classes / functions. So you have to change complex to std::complex<T> (don't forget the template parameter!).

first time using .h and multiple .cpp files in one project, errorssss :(

second post on StackOverflow. I just have some general questions as to why my program is acting the way it is, I don't want help in completing it I was just absent from class on Friday and apparently I missed a lot.
I'm tasked to design a program that contains 3 .cpp and 2 .h files, in essence it will search and sort through arrays of strings using the bubble sort, insertion sort, selection sort methods and sequential and binary search. We are then supposed to benchmark each method to figure out which is the fastest.
I am just confused as to why the compiler keeps yelling at me, it's not making much sense I've been sitting here for about an hour fiddling around with different options or typing the code in differently but to no avail.
My header file
const int NOT_FOUND = -1;
int sequentialSearch(string a[], string needle, int length );
JohnSearch.cpp
#include "JohnSearch.h"
#include <string>
int sequentialSearch(string copied[], string needle, int length)
{
int i; // iteration variable
// look at every element to see if it is the same as needle
for( i = 0; i < length; i++ )
if( copied[i] == needle )
return i;
return NOT_FOUND;
}
TestSearch.cpp
#include "JohnSearch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
** printArray(title,a,length): print out title and then the contents of array a
*/
void printArray(string title, string ref[], int length )
{
int i; // array iteration
cout << title << ": \n";
for( i = 0; i < length; i++ )
cout<<setw(20)<<ref[i]<<"\n";
}
int main(void)
{
string reference[]={"John", "Allen", "Kevin", "Elisabeth"};
const int SZ=sizeof(reference)/sizeof(reference[0]);
string copied[SZ];
printArray("Reference", reference, SZ);
// sequential search (on unsorted array)
cout<<"Search.sequential(ref,Kevin):\t"<<sequentialSearch(reference, "Kevin", SZ)<<endl;
system("Pause");
return 0;
}
Errors
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
testjohnsearch.cpp(28): error C3861: 'copyArray': identifier not found
testjohnsearch.cpp(31): error C2064: term does not evaluate to a function taking 3 arguments
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
johnsearch.cpp(7): error C2065: 'string' : undeclared identifier
johnsearch.cpp(7): error C2146: syntax error : missing ')' before identifier 'copied'
johnsearch.cpp(7): error C2374: 'sequentialSearch' : redefinition; multiple initialization
johnsearch.h(2) : see declaration of 'sequentialSearch'
johnsearch.cpp(7): error C2059: syntax error : ')'
johnsearch.cpp(8): error C2143: syntax error : missing ';' before '{'
johnsearch.cpp(8): error C2447: '{' : missing function header (old-style formal list?)
I'm obviously doing something completely and utterly wrong. I need JohnSearch.cpp for JohnSearch.h right? The forward declaration of the function in JohnSearch.h is defined in JohnSearch.cpp so I need those two files correct?
I'm just really confused. The example program we are supposed to modify has 2 .h files and 3 .cpp files. 2 of those .cpp files correspond with the 2 .h files so thats why I assumed I would also need 2 .h files and 3 .cpp files.
String is still undefined.
johnsearch.h(2): error C2065: 'string' : undeclared identifier
Your header file uses string , so you'll need to include <string>, before your declarations. You also need to qualify it as std::string since the string class resides in the std namespace
So your header file becomes:
#include <string>
const int NOT_FOUND = -1;
int sequentialSearch(std::string a[], std::string needle, int length );
(you should also use include guards in your header files)
Your JohnSearch.cpp also uses string, again, since string is in the std namespace, you'll get errors if you don't use std::string
In your TestSearch.cpp, you have a using namespace std; at the top, you could do the same in JohnSearch.cpp too, that way you can use string instead of std::string
When in doubt, simplify. You can boil the code down to something like this:
#include "JohnSearch.h"
void sequentialSearch(string needle)
{
}
and get the same error (and maybe a warning about an unused parameter).
Yes, string is a type of variable, but it's not innate in the C++ language itself, it's in one of the standard libraries, something you have to tell the compiler about:
#include "JohnSearch.h"
#include <string>
using std::string;
void sequentialSearch(string needle)
{
}
In your header file that you include, you need to have the exact same signature than your function in the cpp file.
Also dont forget to #include <string>, and then use a string like : std::string
E.g.
Int function(int number, int number2);
And in your cpp
Int function(int number, int number2)
{
// code
}
Signature is "int function(int, int)".

Getting undeclared Identifier errors in Winsock.h

I am trying to compile a code
#include <Header1.h>
#include "Header2..h"
#include <ctype.h>
Header1.h includes winsock.h and Header2.h includes windows.h
I am using winsock.h instead of winsock2.h because winsock2.h was showing redefinition errors, which is a standard error, but I was not able to fix that using the solutions provided to them.
I have also tried including ws2tcpip.h, but it is giving tons of redefinition errors in winsock.h.
I am getting 12 errors in this Module
error C3861: 'close': identifier not found
error C2664: 'setsockopt' : cannot convert parameter 4 from 'timeval *' to 'const char *'
error C2065: 'socklen_t' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'optionLength'
error C2065: 'optionLength' : undeclared identifier
error C2065: 'optionLength' : undeclared identifier
error C2664: 'setsockopt' : cannot convert parameter 4 from 'int32 *' to 'const char *'
error C2065: 'MSG_WAITALL' : undeclared identifier
error C2664: 'recvfrom' : cannot convert parameter 2 from 'uint8 *' to 'char *'
error C2065: 'ERROR_END_OF_STREAM' : undeclared identifier
error C3861: 'close': identifier not found
error C3861: 'close': identifier not found
Add #include <winsock2.h> or #include <ws2tcpip.h> on the top of includes.
#include <winsock2.h>
#include <Header1.h>
#include "Header2.h"
#include <ctype.h>
winsock.h should be included first. And I notice that your code seems to be unixish code. On windows, closesocket is used for closing socket.
Also, #define WIN32_LEAN_AND_MEAN before anything else. That often helps.
Also, in some cases helps this (issues with ws2tcpip identifier error):
#pragma once
#define _WIN32_WINNT 0x500
#include <winsock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "WS2_32.Lib")