Consider an enum class
enum class FOO
{
A,B,C
};
struct something
{
FOO abc = FOO::A; //Compiler Doesnt like this
}
int main(){
something _something;
return 0;
}
So the compiler doesnt like the initialisation and gives me 3 different errors.
Error C2238 unexpected token(s) preceding ';'
Error C2059 syntax error: '='
Error C2653 'FOO': is not a class or namespace name
Compiler:
Visual C++
Build Tools MsVC142
So am I initialisation the enum incorrectly? or am i completely missing something.
This is the error I'm getting:
Encounter.h(5,26): error C2061: syntax error: identifier 'TestEnemy'
Encounter.cpp(10,1): error C2511: 'void Encounter::startEnc(TestEnemy)': overloaded member function not found in 'Encounter'
Hero and TestEnemy are classes I've defined in other h and cpp files
Encounter.h:
#pragma once
class Encounter
{
public:
void startEnc(Hero player, TestEnemy e1);
};
Encounter.cpp:
void Encounter::startEnc(Hero player, TestEnemy e1)
{
...
}
I suppose the error could be from another part of my code but visual studio seems to suggest that it's something to do with these. I'll share more code if need be; I just didn't want to inflate this post.
I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock.
Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.
class MySeqBuildBlockModule: public SeqBuildBlock
{
friend class SeqBuildBlockIRns;
public:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0)
{
TI1 = TI1_In; //in us
TI2 = TI2_In; //in us
pSBBList2 = pSBBList0;
//SeqBuildBlockIRns myIRns_3(pSBBList2); //Defined in libSBB.h
}
//~MySeqBuildBlockModule(){}
virtual bool prep (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo);
virtual bool run (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo, sSLICE_POS* pSLC);
protected:
private:
long TI1; //in us
long TI2; //in us
SBBList* pSBBList2;
SeqBuildBlockIRns myIRns_3(pSBBList2); // Line 106
};
bool MySeqBuildBlockModule::prep(MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo)
{
NLS_STATUS lStatus;
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest(); //Line 113
// Now we prepare:
lStatus = pSBBList->prepSBBAll(pMrProt, pSeqLim, pSeqExpo, &dEnergyAllSBBs_DK); // Line 116
if (lStatus ) {
cout << "DK: An error has occurred while preparing SBBs"<< endl;
}
return lStatus;
}
I would have like to intiantiate an object "myIRns_3" of a class defined in third party library
SeqBuildBlockIRns myIRns_3(pSBBList2);
and would like to access it from the prep function as:
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();
I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);
ERRORS encountered:
When I tried to do it inside the constructor, I get the following errors:
MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
When I tried to do it in private section, I get the following errors:
MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union
PS: I have marked the line no in the code that I am posting.
Any help would be appreciated.
Regards,
DK
Your problem is that the below is declaring a function:
SeqBuildBlockIRns myIRns_3(pSBBList2);
What you want to do is declare a member:
SeqBuildBlockIRns myIRns_3;
And then in your constructor construct it with the argument pSBBList2:
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In) // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0),
myIRns_3(pSBBList2) // <- construct
{ }
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)".
I need to write something in C++. I have problem with virtual functions.
For example, in header file Human.h I have this:
class Human
{
public:
virtual int Age();
Human();
~Human();
}
In Human.cpp file I have this:
#include<iostream>
#include "Human.h"
int Human::Age()
{
return 0;
}
I get these compile errors:
Error 4 error C2371: 'Human::Age' : redefinition; different basic types c:\users\jan\desktop\testc\testc\human.cpp 5 1 TestC
Error 3 error C2556: 'Human Human::Age(void)' : overloaded function differs only by return type from 'int Human::Age(void)' c:\users\jan\desktop\testc\testc\human.cpp 5 1 TestC
Error 2 error C2628: 'Human' followed by 'int' is illegal (did you forget a ';'?) c:\users\jan\desktop\testc\testc\human.cpp 4 1 TestC
You have forgotten to end the class definition with a ;
It should read
class Human
{
public:
virtual int Age();
Human();
~Human();
};
This will likely make the error go away. Also, always read the compiler's output: Error 2 error C2628: 'Human' followed by 'int' is illegal (did you forget a ';'?) c:\users\jan\desktop\testc\testc\human.cpp 4 1 TestC