即使我包含了strlen
,我在strcpy
和<cstring>
上也遇到了未定义的错误。我正在使用Visual Studio Community17。
这是我到目前为止所做的一切,但无济于事:
using namespace std
std::strlen
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
我为这个错误而疯狂。
这是我的头文件
#ifndef TEXT_H
#define TEXT_H
#include <stdexcept>
#include <iostream>
using namespace std;
class Text
{
public:
// Constructors and operator=
Text(const char* charSeq = "");
Text(const Text& other); // Copy constructor
void operator = (const Text& other); // Assignment
// Destructor
~Text();
// Text operations
int getLength() const; // # characters
char operator [] (int n) const; // Subscript
void clear(); // Clear string
// Output the string structure -- used in testing/debugging
void showStructure() const;
//--------------------------------------------------------------------
// In-lab operations
// toUpper/toLower operations (Programming Exercise 2)
Text toUpper() const; // Create upper-case copy
Text toLower() const; // Create lower-case copy
// Relational operations (Programming Exercise 3)
bool operator == (const Text& other) const;
bool operator < (const Text& other) const;
bool operator > (const Text& other) const;
private:
// Data members
int bufferSize; // Size of the string buffer
char* buffer; // Text buffer containing a null-terminated sequence of characters
// Friends
// Text input/output operations (In-lab Exercise 1)
friend istream& operator >> (istream& input, Text& inputText);
friend ostream& operator << (ostream& output, const Text& outputText);
};
#endif
Cpp文件
#include "Text.h"
#include <cstring>
Text::Text(const char *charSeq)
{
bufferSize = strlen(charSeq) + 1;
buffer = new char[bufferSize];
strcpy(buffer, charSeq);
}
此处是编译器错误C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Platform.targets(67,5): error MSB8020: The build tools for v142 (Platform Toolset = 'v142') cannot be found. To build using the v142 build tools, please install v142 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution".
答案 0 :(得分:1)
不是在std名称空间中晕了吗?
所以它将是std :: strlen()
例如,这对我有用:
#include <iostream>
#include <cstring>
int main()
{
std::string oof = "oof";
std::cout << std::strlen(oof.c_str()) << std::endl;
}